Nemo

Nemo 关注TA

路漫漫其修远兮,吾将上下而求索。

Nemo

Nemo

关注TA

路漫漫其修远兮,吾将上下而求索。

  •  普罗旺斯
  • 负责帅就完事了
  • 写了1,496,113字

该文章投稿至Nemo社区   Java  板块 复制链接


Springboot添加swagger支持

发布于 2017/04/01 14:56 5,199浏览 6回复 4,351

添加maven依赖:

<!-- swagger start -->
<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>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-staticdocs</artifactId>
<version>2.2.2</version>
<scope>test</scope>
</dependency>
<!-- swagger end -->

配置类:


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 org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.RequestHandler;
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;

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan
/**
*
*/
public class SwaggerConfig extends WebMvcConfigurerAdapter {
/**
* Every Docket bean is picked up by the swagger-mvc framework - allowing for multiple
* swagger groups i.e. same code base multiple swagger resource listings.
*/
protected static Class<?> declaringClass(RequestHandler input) {
return input.getHandlerMethod().getMethod().getDeclaringClass();
}
@Bean
public Docket api() {
if (enableSwagger()) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors. any())
.paths(PathSelectors.any())
.build();
} else {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.none())
.paths(PathSelectors.none())
.build();
}
}

public boolean enableSwagger() {
String useSwagger = "true";//ConfigProp.getProp("swagger.useSwagger");
return "true".equals(useSwagger) ? true : false;
}

protected ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Mp API说明").description("")
.version("1.0").build();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (enableSwagger()) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
}

OK,然后编写测试接口:


import com.nfd.omega.model.JsonResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(value = "测试接口", description = "测试接口")
public class TestController {

@ApiOperation(value = "测试接口")
@GetMapping("/")
public String test() {
return "hello word";
}

@ApiOperation(value = "测试接口 - hello")
@GetMapping("/hello")
public JsonResponse<String> hello() {
return JsonResponse.createSuccessResponse("hello");
}
}

启动项目,之后访问:http://localhost:8080/swagger-ui.html即可看到效果。

本文标签
 {{tag}}
点了个评