我试图按照他们提供的指南将Swagger整合到我的Jersey应用程序中:https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-1.X-Project-Setup-1.5
我使用的是Jersy v1.17而我使用的是自定义应用而不是web.xml
以下是我在简单测试项目中失败的所有内容!
public class ResConfig extends DefaultResourceConfig { // custom app
public ResConfig() {
super();
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setSchemes(new String[] { "http" });
beanConfig.setHost("localhost:9998");
beanConfig.setBasePath("/api");
beanConfig.setResourcePackage("test.resources"); // the package containing my resource
beanConfig.setScan(true);
}
}
我拥有的简单资源
@Path("hello")
@Produces("application/json")
public class Res {
public Res() {
}
@GET
public Response getHello() {
Result result = new Result(); // Result is annotated with XmlRootElement and contains one field String str;
result.setStr("hello");
return Response.status(Status.OK).entity(result).build();
}
}
主要:
public static void main(String[] args) throws Exception {
ResConfig resConfig = new ResConfig();
resConfig.getClasses().add(Res.class);
resConfig.getClasses().add(io.swagger.jaxrs.listing.ApiListingResource.class);
resConfig.getClasses().add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
GrizzlyServerFactory.createHttpServer("http://localhost:9998/", resConfig);
System.in.read();
}
POM包含以下内容:
<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey-jaxrs</artifactId>
<version>1.5.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
Jersey Grizzly服务器因启动异常而失败:
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public javax.ws.rs.core.Response io.swagger.jaxrs.listing.ApiListingResource.getListing(javax.ws.rs.core.Application,javax.servlet.ServletConfig,javax.ws.rs.core.HttpHeaders,javax.ws.rs.core.UriInfo,java.lang.String) at parameter at index 1
SEVERE: Method, public javax.ws.rs.core.Response io.swagger.jaxrs.listing.ApiListingResource.getListing(javax.ws.rs.core.Application,javax.servlet.ServletConfig,javax.ws.rs.core.HttpHeaders,javax.ws.rs.core.UriInfo,java.lang.String), annotated with GET of resource, class io.swagger.jaxrs.listing.ApiListingResource, is not recognized as valid resource method.
SEVERE: Missing dependency for field: javax.servlet.ServletContext io.swagger.jaxrs.listing.ApiListingResource.context
Exception in thread "main" com.sun.jersey.spi.inject.Errors$ErrorMessagesException
at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:172)
at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:134)
at com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory.createHttpServer(GrizzlyServerFactory.java:243)
at com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory.createHttpServer(GrizzlyServerFactory.java:137)
我在Google的前两页搜索了答案,但没有任何帮助!
答案 0 :(得分:0)
Swagger使用Servlet API,因此应用程序必须在servlet环境中运行。您配置Grizzly的方式不设置servlet容器。它只是一个HTTP服务器。
您希望使用jersey-grizzly2-servlet
依赖项,并使用GrizzlyWebContainerFactory
来引导应用程序。这将设置一个servlet容器。
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2-servlet</artifactId>
<version>1.19.1</version>
</dependency>
Map<String, String> initParams = new HashMap<>();
initParams.put("javax.ws.rs.Application", "com.package.ResConfig");
GrizzlyWebContainerFactory.create(uri, initParams);