我使用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')
我能够启动网络应用程序,但我徘徊哪个网址是昂首阔步?我尝试使用http://localhost:8080,http://localhost:8080/swagger和http://localhost:8080/swagger-ui.html。但是没有一个可以被访问。
答案 0 :(得分:8)
如果使用Spring MVC而不是JAX-RS实现来实现端点,我认为@EnableSwagger2
注释和springfox
依赖项将起作用。
几个月前我在博客上发表了这篇文章,Microservices using Spring Boot, Jersey Swagger and Docker
基本上,如果您需要记录Jersey实施的端点,则需要:
1) 确保您的Spring Boot应用程序通过以下方式扫描位于特定软件包中的组件(即com.asimio.jerseyexample.config):
@SpringBootApplication(
scanBasePackages = {
"com.asimio.jerseyexample.config", "com.asimio.jerseyexample.rest"
}
)
2)Jersey配置类实现:
package com.asimio.jerseyexample.config;
...
@Component
public class JerseyConfig extends ResourceConfig {
@Value("${spring.jersey.application-path:/}")
private String apiPath;
public JerseyConfig() {
// Register endpoints, providers, ...
this.registerEndpoints();
}
@PostConstruct
public void init() {
// Register components where DI is needed
this.configureSwagger();
}
private void registerEndpoints() {
this.register(HelloResource.class);
// Access through /<Jersey's servlet path>/application.wadl
this.register(WadlResource.class);
}
private void configureSwagger() {
// Available at localhost:port/swagger.json
this.register(ApiListingResource.class);
this.register(SwaggerSerializers.class);
BeanConfig config = new BeanConfig();
config.setConfigId("springboot-jersey-swagger-docker-example");
config.setTitle("Spring Boot + Jersey + Swagger + Docker Example");
config.setVersion("v1");
config.setContact("Orlando L Otero");
config.setSchemes(new String[] { "http", "https" });
config.setBasePath(this.apiPath);
config.setResourcePackage("com.asimio.jerseyexample.rest.v1");
config.setPrettyPrint(true);
config.setScan(true);
}
}
3)使用JAX-RS(Jersey)和Swagger注释的资源实现:
package com.asimio.jerseyexample.rest.v1;
...
@Component
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "Hello resource", produces = "application/json")
public class HelloResource {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloResource.class);
@GET
@Path("v1/hello/{name}")
@ApiOperation(value = "Gets a hello resource. Version 1 - (version in URL)", response = Hello.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Hello resource found"),
@ApiResponse(code = 404, message = "Hello resource not found")
})
public Response getHelloVersionInUrl(@ApiParam @PathParam("name") String name) {
LOGGER.info("getHelloVersionInUrl() v1");
return this.getHello(name, "Version 1 - passed in URL");
}
...
}
4)确保您的应用程序的Spring Boot配置文件区分Spring MVC(用于执行器端点)和Jersey(用于资源)端点:
<强> application.yml 强>
...
# Spring MVC dispatcher servlet path. Needs to be different than Jersey's to enable/disable Actuator endpoints access (/info, /health, ...)
server.servlet-path: /
# Jersey dispatcher servlet
spring.jersey.application-path: /api
...
答案 1 :(得分:1)
我建议使用依赖关系并启动。在Spring Boot中使用Swagger相当容易,但是要与Jersey一起使用时可能会很棘手。
我们要做的第一件事是配置Swagger以与Jersey一起使用并按如下所示更新您的Jersey配置。
Jersey Config是我们需要进行所有配置的地方
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;
import javax.annotation.PostConstruct;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {
@Autowired
public JerseyConfig(ObjectMapper objectMapper) {
packages("com.rest.resources");
// u can also specify resource class names like below
//register(UserResource.class)
//register(AdminResource.class)
}
@PostConstruct
public void init() {
// Register components where DI is needed
this.SwaggerConfig();
}
private void SwaggerConfig() {
this.register(ApiListingResource.class);
this.register(SwaggerSerializers.class);
BeanConfig swaggerConfigBean = new BeanConfig();
swaggerConfigBean.setConfigId("Swagger Jersey Example");
swaggerConfigBean.setTitle("Using Swagger ,Jersey And Spring Boot ");
swaggerConfigBean.setVersion("v1");
swaggerConfigBean.setContact("DeveloperName");
swaggerConfigBean.setSchemes(new String[] { "http", "https" });
swaggerConfigBean.setBasePath("/api");
swaggerConfigBean.setResourcePackage("com.rest.resources");
swaggerConfigBean.setPrettyPrint(true);
swaggerConfigBean.setScan(true);
}
现在我们已经完成了所有配置,我们需要验证swagger.json是否成功生成。这是重要的步骤,请验证以下URL。
http://localhost:PORT/baseContext/api/swagger.json
由于我们将@Application路径指定为“ / api”,并且尚未配置任何上下文,因此我们的基本上下文为“ /”。因此,我们以 http://localhost:8080/api/swagger.json。
现在,我们的swagger.json已加载,我们现在需要配置Swagger UI。 就像静态HTML或简单Web应用程序中的资源访问一样。
如果不确定如何配置,请 visit this article,在文章结尾处说明如何加载swagger-UI。
答案 2 :(得分:0)
也许您使用相当旧版本的springfox,但现在(版本2.4.0)必须配置与您的代码完全不同,请参阅http://springfox.github.io/springfox/docs/current/
例如,我有以下springfox配置:
@Configuration
@EnableSwagger2
class SwaggerConfig {
@Bean
Docket rsApi() {
new Docket(DocumentationType.SWAGGER_12)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage('com.test.service'))
.paths(PathSelectors.any())
.build()
.pathMapping('/')
.useDefaultResponseMessages(false)
}
private ApiInfo apiInfo() {
new ApiInfoBuilder()
.title('Test API')
.description('Test API')
.version('0.0.10.SNAPSHOT')
.termsOfServiceUrl('')
.contact('Test company')
.license('Public')
.licenseUrl('http://example.com/')
.build()
}
}