該文章已更新,請查看:
http://www.cnblogs.com/yzlpersonal/p/6420507.html
demo地址:demo-swagger-springboot
springboot中swaggerUI的使用
1.pom文件中添加swagger依賴
2.從github項目中下載swaggerUI
然后把dist目錄下的所有文件復制到springboot項目的webapp下面,修改index.html中的: url = "http://petstore.swagger.io/v2/swagger.json";為---------> url="http://localhost:8080/api-docs";
3.spring boot啟動類
public static void main(String[] args) {
SpringApplication.run(IndexController.class,args);
}
/** 配置 swagger開始*/ private SpringSwaggerConfig springSwaggerConfig;
/**
* Required to autowire SpringSwaggerConfig
*/
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
this.springSwaggerConfig = springSwaggerConfig;
}
/**
* Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
* framework - allowing for multiple swagger groups i.e. same code base
* multiple swagger resource listings.
*/
@Bean
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(".*?");
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo(
"測試接口文檔(標題)", // App Service API
"這是一個描述信息。。。",
"termsOfServiceUrl",
"younaame@yourmail.com",
"2222222222222222222222222222",
"3333333333333333333333333333");
return apiInfo;
}
/** 配置 swagger結束*/
4. 添加測試rest接口類:
@RestController
public class HelloController {
@RequestMapping(value = "/hello")
@ApiOperation(value = "添加用戶",httpMethod ="POST", response = User.class,notes = "HelloWorld")
public User hello(@ApiParam(required = true,name = "paramData",value = "用戶信息 json 數據") String paramData){
User userBean = new User();
userBean.setName("測試用戶"); userBean.setOtherInfo("其他信息"); return userBean; } }
5 訪問http://localhost:8080/api-docs,返回:
{
"apiVersion": "1.0",
"apis": [
{
"description": "Basic Error Controller",
"path": "/default/basic-error-controller",
"position": 0
},
{
"description": "Hello Controller",
"path": "/default/hello-controller",
"position": 0
}
],
"authorizations": {
},
"info": {
"contact": "younaame@yourmail.com",
"description": "這是一個描述信息。。。",
"license": "2222222222222222222222222222",
"licenseUrl": "3333333333333333333333333333",
"termsOfServiceUrl": "termsOfServiceUrl",
"title": "測試接口文檔(標題)"
},
"swaggerVersion": "1.2"
} 說明配置正確。 然后再訪問:http://localhost:8080/swagger/index.html,查看restful風格接口信息和效果。