springmvc+swagger2線上文件生成(maven專案)
1.匯入jar包
需要匯入springfox-swagger2和springfox-swagger-ui
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
相關jar包
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>
2.配置基類
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
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 xiegx
* @version 建立時間:2016-8-16 下午2:01:10
* SwaggerUI配置
*/
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages ={"com.era.controller"})
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.era.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("永高專案")
.description("永高專案後臺介面文件")
.termsOfServiceUrl("http://honghu.com")
.contact(new Contact("", "", "HongHu"))
.version("1.1.0")
.build();
}
}
然後再springmvc的配置檔案中配置<bean class="com.era.biz.SwaggerConfig" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />
3.從官網或者github上下載對應的swagger-ui壓縮包
將從官網下載的包解壓縮然後放到webapp下資料夾命名為swagger
4.在web.xml中配置靜態檔案的路徑
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/swagger/*</url-pattern>
</servlet-mapping>
5.在controller中加入相應的註解用來進行測試
@Controller
@Api(value="使用者管理")
public class UserController {
@Autowired
private UserBiz userBiz;
/*
* @ApiOperation(value = "介面說明", httpMethod ="介面請求方式", response ="介面返回引數型別", notes ="介面釋出說明"
*
* @ApiParam(required = "是否必須引數", name ="引數名稱", value ="引數具體描述"
*/
//獲取所有的使用者型別
@ResponseBody
@RequestMapping(value="/getAllUserType.do",method=RequestMethod.POST)
@ApiOperation(value="介面說明(測試)",httpMethod="POST",notes="在沒有會話、沒有簽名的情況下,進入方法體",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
public Response<? extends Object> getAllUserType(){
Response<? extends Object> response=userBiz.getAllUserType();
return response;
}
}
6.修改swagger資料夾下的dist/index.html檔案修改url為http://localhost:8080/ERAPIPE/v2/api-docs
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://localhost:8080/ERAPIPE/v2/api-docs";
}
hljs.configure({
highlightSizeThreshold: 5000
});
// Pre load translate...
if(window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
window.swaggerUi = new SwaggerUi({
7.輸入地址http://localhost:8080/專案名稱/swagger/dist/index.html進行訪問
備註:使用的swagger版本為3.0.21