一、先建立swagger的java文件
package com.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
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;
//让spring来加载该类
@Configuration
//启动swagger2
@EnableSwagger2
//引入配置类
@EnableWebMvc
public class SwaggerConfig {
@Bean
public Docket buildDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInf())
.select()
.apis(RequestHandlerSelectors.basePackage("com"))//controller路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInf(){
return new ApiInfoBuilder()
.title("接口列表")
.termsOfServiceUrl("http://127.0.0.1:8080/swagger-ui.html")
.description("springmvc swagger 接口测试")
.version("1.0.0")
.build();
}
}
二、在spring-mvc.xml文件下加上swagger引用
//引入的swagger在项目中的实际位置
<mvc:resources mapping="swagger-ui.html**" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**"
location="classpath:/META-INF/resources/webjars/" />
三、实体类加上注解
//配置实体类名
@ApiModel(value="Architecture")
//实体类参数(value:swagger显示的属性名,hidden:可以理解为是否隐藏)
@ApiModelProperty(value="ID",hidden=true)
四、在ctroller层添加注解
//swagger上显示的接口名与备注名(description设置备注名,设置备注名的参数还有tags,这个我设置之后地址就不对了)
@Api(value="Architecture",description="设置建筑数据")
//controller与restcontroller的区别是,controller默认返回String是返回到该String的页面,需要返回数据需要加@requestbody注解,而restcontroller是默认返回数据
@RestController
//调用该方法的url地址
@RequestMapping("/architecture")
//swagger上显示的方法名
@ApiOperation(value = "插入建筑数据", notes = "插入建筑数据")