我正在使用springboot jersey进行web restful实现 . 现在我要将swagger整合到我们的应用程序中 . 我做了以下 .
@Configuration
@EnableSwagger2
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration(){
register(HelloworldAPI.class);
configureSwagger();
}
private void configureSwagger() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setBasePath("/");
beanConfig.setResourcePackage("com.cooltoo.api");
beanConfig.setPrettyPrint(true);
beanConfig.setScan(true);
}
}
我在build.gradle上添加了以下依赖项:
compile('io.springfox:springfox-swagger2:'+springfoxSwaggerVersion)
compile('io.springfox:springfox-petstore:'+springfoxSwaggerVersion)
compile('io.springfox:springfox-swagger-ui:'+springfoxSwaggerVersion)
compile('io.swagger:swagger-jersey2-jaxrs:1.5.8')
我能够启动Web应用程序,但我徘徊哪个url是为了招摇?我尝试了http://localhost:8080,http://localhost:8080/swagger和http://localhost:8080/swagger-ui.html . 但是没有一个可以被访问 .
3 回答
我认为如果使用Spring MVC而不是JAX-RS实现来实现 endpoints ,那么
@EnableSwagger2
注释和springfox
依赖项将会起作用 .几个月前我在博客上发表了这篇文章,Microservices using Spring Boot, Jersey Swagger and Docker
基本上,如果您需要记录Jersey实现的 endpoints ,则需要:
1)确保您的Spring Boot应用程序通过以下方式扫描位于特定软件包中的组件(即com.asimio.jerseyexample.config):
2)Jersey配置类实现:
3)使用JAX-RS(Jersey)和Swagger注释的资源实现:
4)确保您的应用程序的Spring Boot配置文件区分Spring MVC(用于 Actuator endpoints )和Jersey(用于资源) endpoints :
application.yml
我建议使用Simply Swagger Jersey JAXRS依赖并开始 . 使用Swagger和Spring Boot相当容易,但是当使用Jersey时,它可能很棘手 .
我们需要做的第一件事是将Swagger配置为与Jersey一起使用并更新您的Jersey Config,如下所示 .
Jersey Config是我们需要进行所有配置的地方
现在我们已经完成了所有配置,我们需要验证是否成功生成了swagger.json . 这是一个重要的步骤,验证以下URL .
因为我们已经将@Application Path作为"/api"并且我们没有配置任何上下文所以我们的基本上下文是"/" .Hence我们访问url为http://localhost:8080/api/swagger.json .
现在我们加载了swagger.json,现在我们需要配置Swagger UI . 它就像一个简单的Web应用程序中的静态HTML或资源访问 .
如果您不确定如何配置,请visit this article说明如何在文章末尾加载swagger-UI .
也许您使用相当旧版本的springfox,但现在(对于版本2.4.0)它必须配置与您的代码非常不同,请参阅http://springfox.github.io/springfox/docs/current/
例如,我有以下springfox配置: