swagger生成api文檔蛋勺,先看一下效果
swagger 對(duì)于userController的接口的描述
1.使用
1.1pom引入
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger -->
1.2 @EnableSwagger2
@SpringBootApplication
@EnableSwagger2
public class DemoApplication {
public static void main(String[] args){
SpringApplication.run(DemoApplication.class,args);
}
}
1.3 用中文進(jìn)行詳細(xì)描述
1.3.1 對(duì)接口進(jìn)行描述
/**
* 在url中使用正則表達(dá)式
* @param id
* @return
*/
@GetMapping("/{id:\\d+}")
@JsonView(User.UserDetailView.class)
@ApiOperation(value = "獲取用戶(hù)詳情")
public User get(@ApiParam(value = "用戶(hù)id") @PathVariable String id){
//throw new UserNotFoundException(id);
System.out.println(id);
User user = new User();
user.setUsername("tom");
return user;
}
- 使用
@ApiOperation(value = "獲取用戶(hù)詳情")
對(duì)接口進(jìn)行描述
1.3.2 對(duì)字段進(jìn)行描述
- 使用
@ApiModelProperty
進(jìn)行描述
public class UserQueryCondition {
@ApiModelProperty(value = "用戶(hù)名")
private String username;
@ApiModelProperty(value = "年齡起始值")
private int age;
@ApiModelProperty(value = "年齡終止值")
private int ageTo;
- 使用
@ApiParam
進(jìn)行描述
@DeleteMapping("/{id:\\d+}")
@ApiOperation(value = "刪除用戶(hù)信息")
public void delete(@ApiParam(value = "用戶(hù)Id") @PathVariable String id){
}
2.訪問(wèn)
- 訪問(wèn)端點(diǎn)
/swagger-ui.html
例如:http://localhost:8080/swagger-ui.html#/