The use of swagger in spring boot

Keywords: Attribute Spring SpringBoot

Look, learn

  • Import dependency
<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>

It should be noted that the parent module or dependent module or has its own web starter, that is:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Otherwise start error reporting.

  • swagger configuration class
@Configuration   //Declare that this class is a configuration class
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("springboot.model_swagger.controller"))  //This place is the path where the controller package is located, and it can be changed according to your situation.
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("eboot-api File")
                .description("For more information, please visit https://www.jianshu.com/u/3979cb11f079")
                .termsOfServiceUrl("https://gitee.com/QuanZhanZhiLu/easy-boot")
                .version("1.0")
                .build();
    }
}

In addition, you need to add @EnableSwagger2 annotation to the application class to turn on the swagger function:

  • Add the controller class under the controller package and add methods (the annotations used will be explained later)
@RestController
@Api(value = "TestController")
public class TestController {


    @GetMapping("print1")
    @ApiOperation(value = "Printing key and value",notes = "Remarks")
    @ApiImplicitParam(paramType="query", name = "str", value = "Who is a string?", required = true, dataType = "String")
    public Map<String, String> printOne(@RequestParam String str) {
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("key", "value");
        return resultMap;
    }


    @PostMapping("print2")
    @ApiOperation(value = "Print multiple pieces of data",notes = "Remarks")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "str1",value = "Who is a string?",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "str2",value = "Who is a string?",required = true,dataType = "String")
    })
    public Map<String,String> printTwo(@RequestParam String str1,@RequestParam String str2){
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("key1",str1);
        resultMap.put("key2",str2);
        return resultMap;
    }

    //Accept object parameters
    @PostMapping("saveDoctor")
    @ApiOperation(value = "Preserving Doctor Information")
    public Map<String,String> saveDoctor(@RequestBody Doctor doctor){
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("key",doctor.getName());
        return resultMap;
    }

    @PostMapping("updateDoctor/{doctorId}")
    @ApiOperation(value = "Modify the doctor's information")
    @ApiImplicitParam(paramType = "query",name = "doctorId",value = "Doctors id",required = true,dataType = "String")
    public Doctor updateDoctor(@RequestParam String doctorId,@RequestBody Doctor doctor){
        return doctor;
    }

}

Annotation analysis:

If an object is used as a parameter in the method, such as:

So the Doctor class should be like this:

@ApiModel(value = "Doctor Object Model")    //Declare that the object is a swagger template object
public class Doctor {
    @ApiModelProperty(value = "id",required = true)    //Class attributes are annotated with this annotation to declare that the attribute is a template object attribute
    private Integer id;
    @ApiModelProperty(value = "Name of doctor",required = true)
    private String name;

    public Doctor() {
    }

    public Doctor(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Doctor{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
  • Visit swagger: localhost:8080/swagger-ui.html



Posted by leewad on Mon, 07 Oct 2019 11:29:05 -0700