首先pom.xml添加依賴?
<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>
在Application.java同級創(chuàng)建Swagger2的配置類Swagger2
@Configuration
@EnableSwagger2
publicclassSwagger2{
@Bean
publicDocketcreateRestApi(){
returnnewDocket(DocumentationType.SWAGGER_2)
? ? ? ? ? ? ? ? .apiInfo(apiInfo())
? ? ? ? ? ? ? ? .select()
????????????????.apis(RequestHandlerSelectors.basePackage("com.didispace.web"))
? ? ? ? ? ? ? ? .paths(PathSelectors.any())
? ? ? ? ? ? ? ? .build();
? ? }
privateApiInfoapiInfo(){
returnnewApiInfoBuilder()
.title("Spring Boot中使用Swagger2構(gòu)建RESTful APIs")
.description("更多Spring Boot相關(guān)文章請關(guān)注:廣告鏈接")
.termsOfServiceUrl(" 廣告鏈接? url")
.contact("XXX")
.version("1.0")
? ? ? ? ? ? ? ? .build();
? ? }
}
@Configuration是用來讓springboot來加載該配置的
然后@EnableSwagger2這個是使用swageer注解祸轮。
@RestController
@RequestMapping(value="/users")// 通過這里配置使下面的映射都在/users下,可去除
publicclassUserController{
staticMap users = Collections.synchronizedMap(newHashMap());
@ApiOperation(value="獲取用戶列表", notes="")
@RequestMapping(value={""}, method=RequestMethod.GET)
publicListgetUserList(){
List r =newArrayList(users.values());
returnr;
? ? }
@ApiOperation(value="創(chuàng)建用戶", notes="根據(jù)User對象創(chuàng)建用戶")
@ApiImplicitParam(name ="user", value ="用戶詳細實體user", required =true, dataType ="User")
@RequestMapping(value="", method=RequestMethod.POST)
publicStringpostUser(@RequestBody User user){
? ? ? ? users.put(user.getId(), user);
return"success";
? ? }
@ApiOperation(value="獲取用戶詳細信息", notes="根據(jù)url的id來獲取用戶詳細信息")
@ApiImplicitParam(name ="id", value ="用戶ID", required =true, dataType ="Long")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
publicUsergetUser(@PathVariable Long id){
returnusers.get(id);
? ? }
@ApiOperation(value="更新用戶詳細信息", notes="根據(jù)url的id來指定更新對象逊脯,并根據(jù)傳過來的user信息來更新用戶詳細信息")
@ApiImplicitParams({
@ApiImplicitParam(name ="id", value ="用戶ID", required =true, dataType ="Long"),
@ApiImplicitParam(name ="user", value ="用戶詳細實體user", required =true, dataType ="User")
? ? })
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
publicStringputUser(@PathVariable Long id, @RequestBody User user){
? ? ? ? User u = users.get(id);
? ? ? ? u.setName(user.getName());
? ? ? ? u.setAge(user.getAge());
? ? ? ? users.put(id, u);
return"success";
? ? }
@ApiOperation(value="刪除用戶", notes="根據(jù)url的id來指定刪除對象")
@ApiImplicitParam(name ="id", value ="用戶ID", required =true, dataType ="Long")
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
publicStringdeleteUser(@PathVariable Long id){
? ? ? ? users.remove(id);
return"success";
? ? }
}
代碼部分結(jié)束宣增,然后迫不及待的去訪問http://localhost:8080/swagger-ui.html,會報這個錯
說明 /swagger-ui.html 還沒有映射到我們下載的swagger皮仁。
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
????????????????registry.addResourceHandler("swagger-ui.html")
? ? ? ? ? ? ? ? .addResourceLocations("classpath:/META-INF/resources/");
}
最后自定義的 類 (繼承WebMvcConfigurerAdapter)類擅耽,生效的