1、引入POM依赖
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2、添加swagger config配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//swagger要扫描的包路径
.apis(RequestHandlerSelectors.basePackage("com.test.swagger"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot Swagger 测试")
.description("Springboot 整合Swagger2")
.termsOfServiceUrl("localhost:8080")
.contact(new Contact("Swagger测试","localhost:8080/swagger-ui.html","baidu@qq.com"))
.version("1.0")
.build();
}
}
3、接口类
@Api(description = "用户接口")
@RestController
@RequestMapping("/user")
public class TestController {
@ApiOperation(value = "获取用户11", notes = "根据id查询用户信息11", produces = MediaType.APPLICATION_JSON_VALUE)
//@ApiImplicitParam(name = "req", value = "用户查询请求", required=true, dataTypeClass = UserReq.class)
@PostMapping(value = "/queryUser")
public UserInfo queryRuleUser(@RequestBody UserReq req) {
UserInfo resp = new UserInfo();
return resp;
}
@ApiOperation(value = "获取用户222", notes = "根据id查询用户信息22", produces = MediaType.APPLICATION_JSON_VALUE)
//@ApiImplicitParam(name = "req", value = "用户查询请求", required=true, dataTypeClass = UserReq.class)
@PostMapping(value = "/queryUser2")
public UserInfo queryRuleUser2(@ModelAttribute UserReq req) {
UserInfo resp = new UserInfo();
return resp;
}
}
4、实体类
@ApiModel
public class UserReq implements Serializable {
@ApiModelProperty(value = "用户id", name = "id")
private int id;
@ApiModelProperty(value = "用户name", name = "name", required = true)
private String name;
@ApiModelProperty(value = "用户age", name = "age")
private int age;
@ApiModelProperty(value = "城市信息")
private CityInfo cityInfo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public CityInfo getCityInfo() {
return cityInfo;
}
public void setCityInfo(CityInfo cityInfo) {
this.cityInfo = cityInfo;
}
}
网友评论