參考文章地址:https://www.cnblogs.com/nihaofenghao/p/7633018.html
1、加入依賴
<!-- springfox依賴 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
2、spring-mvc.xml 加入下面的代碼
<!-- Enables swgger ui-->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
3、swagger2配置類
package com.swagger.controller.config;
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 springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableWebMvc// spring mvc框架必須加上這個註釋
@EnableSwagger2
@ComponentScan(basePackages="com.swagger.controller")
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(this.apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))//改成api接口所在包
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
@SuppressWarnings("deprecation")
ApiInfo info=new ApiInfo(
"Spring 構建RestFule",
"aaa",
"aa",
"a",
"cc",
"x",
"x");
return info;
}
}
4、編寫接口
package com.swagger.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api("測試類")
@Controller
public class SwaggerController {
@ApiOperation(value="測試方法",httpMethod="GET")
@RequestMapping(value="/test",method=RequestMethod.GET)
@ResponseBody
public String hello(){
return "hello word!!!";
}
}
完畢。
小結:
1、@EnableWebMvc// spring mvc框架必須加上這個註釋,第一次搜的例子是spring boot框架的,沒有這句話,接口頁面打開是空白的。
2、spring-mvc.xml中加入的兩句話不要漏掉,我是漏掉了第一段,然後接口頁面報404錯誤
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
3、記得改成api接口所在包
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(this.apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))//改成api接口所在包
.paths(PathSelectors.any())
.build();
}