Hello你們好,本章咱們添加Swagger2來在線自動生成接口的文檔+測試功能。有問題能夠聯繫我mr_beany@163.com。另求各路大神指點,感謝
Swagger是一款經過咱們添加的註解來對方法進行說明,來自動生成項目的在線api接口文檔的web服務。
javascript
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>複製代碼
而後鼠標右鍵選擇Maven→Reimport進行依賴下載
css
在文件夾configurer中建立SwaggerConfigurerhtml
package com.example.demo.core.configurer;
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;
/**
* @author 張瑤
* @Description:Swagger2 配置文件
* @time 2018/4/20 22:42
*/
@Configuration
@EnableSwagger2
public class SwaggerConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("mySpringBoot 使用Swagger2構建RESTful APIs")
.description("更多Spring Boot相關文章請關注:https://juejin.im/user/59e7fb9451882578e1406a51/posts")
.termsOfServiceUrl("https://juejin.im/user/59e7fb9451882578e1406a51/posts")
.contact(new Contact("Mr_初晨", "https://gitee.com/beany/mySpringBoot", null))
.version("1.0")
.build();
}
}複製代碼
package com.example.demo.controller;
@RestController
@RequestMapping("userInfo")
@Api(tags = {"用戶操做接口"}, description = "userInfoControler")
public class UserInfoController {
@Resource
private UserInfoService userInfoService;
@PostMapping("/hello")
public String hello() {
return "hello SpringBoot";
}
@ApiOperation(value = "查詢用戶", notes = "根據用戶ID查詢用戶")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用戶ID", required = true,
dataType = "Integer", paramType = "query")
})
@PostMapping("/selectById")
public RetResult<UserInfo> selectById(@RequestParam Integer id) {
UserInfo userInfo = userInfoService.selectById(id);
return RetResponse.makeOKRsp(userInfo);
}
@PostMapping("/testException")
public RetResult<UserInfo> testException(Integer id) {
List a = null;
a.size();
UserInfo userInfo = userInfoService.selectById(id);
return RetResponse.makeOKRsp(userInfo);
}
}
複製代碼
注意參數前需加上@RequestParam
java
以上註解你們能夠查看參考swagger官方註解文檔進行自定義添加jquery
瀏覽器輸入localhost:8080/swagger-ui.html
咱們能夠看到。。哎呀我曹,頁面呢??git
繼承WebMvcConfigurationSupport
以後,靜態文件映射會出現問題,須要從新指定靜態資源github
在WebConfigurer
中添加以下代碼web
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/favicon.ico")
.addResourceLocations("classpath:/META-INF/resources/favicon.ico");
super.addResourceHandlers(registry);
}複製代碼
瀏覽器輸入localhost:8080/swagger-ui.html
咱們能夠看到以下頁面spring
打開POST /userInfo/selectByIdjson
請求結果
根據swagger官方使用手冊 找到關於本地化和翻譯的說明:
在resourece
目錄下建立\META-INF\resourece
目錄,建立swagger-ui.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="icon" type="image/png" href="webjars/springfox-swagger-ui/images/favicon-32x32.png" sizes="32x32"/>
<link rel="icon" type="image/png" href="webjars/springfox-swagger-ui/images/favicon-16x16.png" sizes="16x16"/>
<link href='webjars/springfox-swagger-ui/css/typography.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='webjars/springfox-swagger-ui/css/reset.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='webjars/springfox-swagger-ui/css/screen.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='webjars/springfox-swagger-ui/css/reset.css' media='print' rel='stylesheet' type='text/css'/>
<link href='webjars/springfox-swagger-ui/css/print.css' media='print' rel='stylesheet' type='text/css'/>
<script src='webjars/springfox-swagger-ui/lib/jquery-1.8.0.min.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/jquery.slideto.min.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/jquery.wiggle.min.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/jquery.ba-bbq.min.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/handlebars-2.0.0.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/underscore-min.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/backbone-min.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/swagger-ui.min.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/highlight.7.3.pack.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/jsoneditor.min.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/marked.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lib/swagger-oauth.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/springfox.js' type='text/javascript'></script>
<!--國際化操做:選擇中文版 -->
<script src='webjars/springfox-swagger-ui/lang/translator.js' type='text/javascript'></script>
<script src='webjars/springfox-swagger-ui/lang/zh-cn.js' type='text/javascript'></script>
</head>
<body class="swagger-section">
<div id='header'>
<div class="swagger-ui-wrap">
<a id="logo" href="http://swagger.io">swagger</a>
<form id='api_selector'>
<div class='input'>
<select id="select_baseUrl" name="select_baseUrl"/>
</div>
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/>
</div>
<div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div>
<div class='input'><a id="explore" href="#" data-sw-translate>Explore</a></div>
</form>
</div>
</div>
<div id="message-bar" class="swagger-ui-wrap" data-sw-translate> </div>
<div id="swagger-ui-container" class="swagger-ui-wrap"></div>
</body>
</html>複製代碼
重點爲<!--國際化操做:選擇中文版 -->
下兩個js文件
translator.js
爲翻譯器 zh-cn.js
爲中文腳本語言
瀏覽器輸入localhost:8080/swagger-ui.html
咱們能夠看到以下頁面
碼雲地址: gitee.com/beany/mySpr…
GitHub地址: github.com/MyBeany/myS…
寫文章不易,如對您有幫助,請幫忙點下star
springboot添加Swagger2來在線自動生成接口的文檔+測試功能已完成,後續功能接下來陸續更新,有問題能夠聯繫我mr_beany@163.com。另求各路大神指點,感謝你們。