Spring Boot 2.1.x Swagger2填坑笔记
1.MAVEN依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2. 启用Swagger、配置Swagger信息
@EnableSwagger2
@SpringBootApplication
public class FullscreenApplication {
public static void main(String[] args) {
SpringApplication.run(FullscreenApplication.class, args);
}
}
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Api Docs")
.contact(new Contact("宇智波鹏", "uchiha-peng.cc", "uchiha-peng@outlook.com"))
.description("接口文档V0.1")
.version("0.1")
.build();
}
}
3. 解决部署后访问Swagger反复弹窗
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")//设置允许跨域请求的域名
.allowCredentials(true)//是否允许证书 不再默认开启
.allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS")//设置允许的方法
.allowedHeaders("*")
.maxAge(3600);//跨域允许时间
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
AntPathMatcher matcher = new AntPathMatcher();
matcher.setCaseSensitive(false);
configurer.setPathMatcher(matcher);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
// 解决swagger的js文件无法访问
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(FullscreenApplication.class);
}
}
4. 控制器和方法的注释(@Api(tags = “注释”)
@RestController
@RequestMapping("/api")
@Api(tags = "大屏展示数据相关接口")
public class ApiController {
@ApiOperation("查询所有硬件数量")
@GetMapping("/getHardware")
public ResponseEntity<Hardware> getHardware() {
try {
Hardware hardware = new Hardware( 2000, 1800, 3500);
if (hardware != null)
return ResponseEntity.status(HttpStatus.OK).body(hardware);
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
}
5. 实体的注释(实体必须驼峰命名,小写开头,否则不显示)
@ApiModel(value = "Hardware",description = "硬件计数")
public class Hardware extends BaseModel implements Serializable {
@ApiModelProperty("红绿灯")
private int trafficLight;
@ApiModelProperty("RSU")
private int rsu;
@ApiModelProperty("摄像头")
private int camera;
public int getTrafficLight() {
return trafficLight;
}
public void setTrafficLight(int trafficLight) {
this.trafficLight = trafficLight;
}
public int getRsu() {
return rsu;
}
public void setRsu(int rsu) {
this.rsu = rsu;
}
public int getCamera() {
return camera;
}
public void setCamera(int camera) {
this.camera = camera;
}
public Hardware() {
}
public Hardware(int trafficLight, int rsu, int camera) {
this.trafficLight = trafficLight;
this.rsu = rsu;
this.camera = camera;
}
}