springBoot 集成swagger2.9.2

时间:2023-07-23 22:29:38
springBoot 集成swagger2.9.2

加依赖

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

springBoot的application.java的同级目录下新建SwaggerConfiguration类

package com.example.demo;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) //过滤的接口
.paths(PathSelectors.any())
.build();
} private ApiInfo getApiInfo() {
// 定义联系人信息
Contact contact = new Contact("name","https://baidu.com", "test@test.com");
return new ApiInfoBuilder()
.title("标题")
.description("描述")
.version("版本")
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
.contact(contact)
.build();
}
}

controller类:

@RestController
@Api(value = "/user",description = "这个是用户信息 ",tags = "用户信息")
@RequestMapping("/user")
public class UserContrller { @ApiOperation(value="获取用户列表", notes="")
@GetMapping("")
public List<User> getUserList() {
return null;
} @ApiOperation(value="创建用户", notes="根据User对象创建用户")
@PostMapping("")
public String postUser(@RequestBody User user) {
return "success";
}
}

浏览器输入 http://localhost:8080/swagger-ui.html  访问

springBoot 集成swagger2.9.2

这样就完成了, 接下来就是去搜Swagger的常用注解怎么使用就ok了。 附上链接: https://github.com/swagger-api/swagger-core/wiki/annotations

——————————————————————————————————————

以下是我遇到的部分问题

@ApiImplicitParam 注解: 如果参数是实体类并且实体类中被@ApiModel和@ApiModelProperty注解修饰过,  @ApiImplicitParam注解就不要加了。

如果,没有实体类没有被@ApiModel和@ApiModelProperty注解修饰过, @ApiImplicitParam可加可不加,  另外在实体类参数之前加上@RequestBody 和不加@RequestBody,swagger的文档参数显示是不一样的

比如不加@RequestBody注解的代码:

@ApiOperation(value="创建用户", notes="根据User对象创建用户")
@PostMapping("")
public String postUser(User user) {
users.put(user.getId(), user);
return "success";
}

User实体:

package com.example.demo.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @ApiModel(value = "用户信息", description = "这个用户信息只用于测试")
@Data
public class User { @ApiModelProperty("id")
private Long id; @ApiModelProperty(value="姓名",required=true)
private String name; @ApiModelProperty("年龄")
private Integer age;
}

swagger显示的参数是这样的

springBoot 集成swagger2.9.2

加了@RequestBody

    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
@PostMapping("")
public String postUser(@RequestBody User user) {
users.put(user.getId(), user);
return "success";
}

加了@RequestBody的swagger:

springBoot 集成swagger2.9.2

springBoot 集成swagger2.9.2