springboot+swagger2
http://start.spring.io/生成springboot工程
引入maven依賴
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
加載swagger2配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Autowired
Environment env;
@Bean
public Docket createRestApi(ApiInfo apiInfo) {
return new Docket(DocumentationType.*SWAGGER_2*)
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage(env.getProperty("swagger.scan")))
.paths(PathSelectors.any())
.build();
}
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(env.getProperty("swagger.title"))
.description(env.getProperty("swagger.description"))
.version(env.getProperty("swagger.version"))
.build();
}
}
application.yml
spring:
application:
name: swagger-test
server:
port: 8080
context-path: /api
swagger:
scan: com.swagger.test
title: 使用者service API
description: 使用者service API
version: 1.0.0
controller
@RestController
@RequestMapping("/user")
@Api(value="使用者controller",description="使用者操作",tags={"使用者操作接口"})
public class UserController {
@GetMapping("/get")
@ApiOperation(value = "擷取使用者接口",notes = "擷取使用者接口notes",response = String.class)
public String get(@ApiParam(name = "userName",value = "使用者名",required = true) @RequestParam("userName") String userName){
return "傳回值:"+userName;
}
@PostMapping("/post")
@ApiOperation(value = "擷取使用者接口Post",notes = "擷取使用者接口notes Post",response = UserForm.class)
public UserForm post(@RequestBody @ApiParam(name="使用者對象",value="傳入json格式",required=true) UserForm userForm){
return userForm;
}
}
form
@ApiModel(description = "使用者from")
public class UserForm {
@ApiModelProperty(value = "使用者名",name = "userName",required = true)
private String userName;
@Override
public String toString() {
return "UserForm{" +
"userName='" + userName + '\'' +
'}';
}
/**
* Getter method for property <tt>userName</tt>
*
* @return property value of userName
*/
public String getUserName() {
return userName;
}
/**
* Setter method for property <tt>userName</tt>.
*
* @param userName value to be assigned to property userName
*/
public void setUserName(String userName) {
this.userName = userName;
}
}
啟動類:
@EnableSwagger2
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
啟動 通路:
http://localhost:8080/api/swagger-ui.html例子下載下傳位址:
http://download.csdn.net/download/zyj_2012/10191998