更新swagger2配置,又要开发新工具了,发现上次存的swager配置居然启动不起来,说明我当时可能没意识到这部分也会成为我未来工作的绊脚石。
@Configuration
@EnableSwagger2
public class SwaggerConfig {
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("API接口文档")
.description("用户信息管理")
.version("1.0.0")
.build();
}
/*
private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES =
new HashSet<String>(Arrays.asList("application/json","application/xml"));
//可以在Bean中Docket对象后面通过produces(DEFAULT_PRODUCES_AND_CONSUMES)来做全局定义
//但我这里不使用该方法
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.XXX.Controller"))
.paths(PathSelectors.any())
.build();
}
}
三个重要的maven:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency>
使用:
@ApiOperation(value = "随便写", notes = "随便写一点",produces= "application/json;charset=UTF-8")
@PostMapping(value = "/自己定义的路径")
public String checkCredit(@RequestParam(value = "key",required = false) String key){
......
}
这里想重点介绍他,produces= “application/json;charset=UTF-8”,可以让返回json显示(我就好奇我上次mongo也没定义这货啊 也是json显示的。。。不懂)