美文网首页
SpringBoot之整合Swagger2生成接口文档

SpringBoot之整合Swagger2生成接口文档

作者: 赵柯里昂 | 来源:发表于2017-10-11 09:13 被阅读0次

1.添加依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>

2.Swagger2配置类

package com.zhizhuoedu.student;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author: zhao.jw
 * @Description:
 * @Date: Created in 2017-10-10 17:25
 * @Modified By:
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zhizhuoedu.student.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("学生管理系统APIS")
                .description("SpringBoot构建项目整合swagger生成API文档")
                .contact("Zhao.jw")
                .version("1.0")
                .build();
    }

}

3.Controller

package com.zhizhuoedu.student.controller;

import com.zhizhuoedu.student.dao.IStudentDao;
import com.zhizhuoedu.student.entity.Student;
import com.zhizhuoedu.student.service.StudentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: zhao.jw
 * @Description:RESTFUL规范对学生信息进行增删改查
 * @Date: Created in 2017-10-10 15:07
 * @Modified By:
 */
@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private IStudentDao studentDao;
    @Autowired
    private StudentService studentService;

    /**
     * @Author: Zhao.jw
     * @Description:获取学生列表
     */
    @ApiOperation(value = "获取学生列表",notes = "")
    @GetMapping("/students")
    public List<Student> getStudents(){
        return studentDao.findAll();
    }

    /**
     * @Author: Zhao.jw
     * @Description:根据学生ID获取学生信息
     */
    @ApiOperation(value = "根据学生ID获取学生信息",notes = "根据学生ID获取学生信息")
    @GetMapping("/students/{id}")
    public Student getStudentById(@PathVariable("id") Integer id){
        return studentDao.findOne(id);
    }

    /**
     * @Author: Zhao.jw
     * @Description:根据学生年龄获取学生列表
     */
    @ApiOperation(value = "根据学生年龄获取学生列表",notes = "")
    @GetMapping("/students/age/{age}")
    public List<Student> getStudentsByAge(@PathVariable("age") Integer age){
        return studentDao.findByAge(age);
    }

    /**
     * @Author: Zhao.jw
     * @Description:添加一个学生
     */
    @ApiOperation(value = "创建学生",notes = "")
    @PostMapping("/students")
    public String addStudent(String name,Integer age){
        Student entity=new Student(name,age);
        return studentDao.saveAndFlush(entity).toString();
    }

    /**
     * @Author: Zhao.jw
     * @Description:根据学生ID更新学生数据
     */
    @ApiOperation(value = "更新学生数据")
    @PutMapping("/students/{id}")
    public String updateStudent(@PathVariable("id") Integer id, String name,Integer age){
        Student entity=new Student(id,name,age);
        return studentDao.saveAndFlush(entity).toString();
    }

    /**
     * @Author: Zhao.jw
     * @Description:根据学生ID删除学生数据
     */
    @ApiOperation(value = "根据学生ID删除学生数据",notes = "")
    @DeleteMapping("/students/{id}")
    public void delete(@PathVariable("id") Integer id){
        studentDao.delete(id);
    }


    /**
     * @Author: Zhao.jw
     * @Description:事务
     */
    @ApiOperation(value = "事务提交",notes = "")
    @GetMapping("/students/two")
    public void studentsTwo(){
            studentService.insertTwo();
    }
}

4.http://127.0.0.1:8080/swagger-ui.html

Paste_Image.png

相关文章

网友评论

      本文标题:SpringBoot之整合Swagger2生成接口文档

      本文链接:https://www.haomeiwen.com/subject/qepiyxtx.html