问题描述
我有一个在嵌入式Grizzly服务器上运行的Jersey2应用程序-与此示例相同的设置: https://github.com/jersey/jersey/tree/2.18/examples/https-clientserver-grizzly/src/main/java/org/glassfish/jersey/examples/httpsclientservergrizzly
I have a Jersey2 application that runs on an embedded Grizzly server - a setup identical to this example:https://github.com/jersey/jersey/tree/2.18/examples/https-clientserver-grizzly/src/main/java/org/glassfish/jersey/examples/httpsclientservergrizzly
我已将其与swagger-jersey2-jaxrs_2.10集成在一起,并且运行正常.既然出现了swagger-core 1.5,并且产生了Swagger 2.0定义,我想升级到该版本.
I have integrated it with swagger-jersey2-jaxrs_2.10 and it has been working OK.Now that swagger-core 1.5 came out and it produces Swagger 2.0 definitions, I would like to upgrade to that version.
已遵循本网站上的Swagger设置说明: https://github .com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 我发现Swagger不再起作用,因为它需要注入ServletContext实例,和用@Context注释的ServletContext字段没有注入到我的项目中(它们显示为null).
Having followed the Swagger setup instructions from this site:https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5I discovered that Swagger won't work any more as it requires a ServletContext instance to be injected,and ServletContext fields annotated with @Context are not being injected in my project (they show as nulls).
所以我的实际问题是:jersey-container-grizzly2-servlet完全支持ServletContext吗?有什么办法可以通过更改项目的配置来获取ServletContext?还是我应该研究不需要ServletContext的集成swagger-core 1.5的方法?
So my actual question is: does jersey-container-grizzly2-servlet support ServletContext at all?Is there any way I can get ServletContext to be injected by altering my project's config?Or should I look into ways of integrating swagger-core 1.5 that don't require a ServletContext?
推荐答案
这就是我的工作方式:
将这些依赖项添加到您的pom.xml中:
Add these dependencies to your pom.xml:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
</dependency>
注册ApiListingResource和SwaggerSerializer:
Register ApiListingResource and SwaggerSerializers:
@ApplicationPath("/")
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(io.swagger.jaxrs.listing.ApiListingResource.class);
classes.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return classes;
}
}
初始化Grizzly,Jersey和Swagger:
Initialize Grizzly, Jersey and Swagger:
public class Main
{
private final static Logger logger = LogManager.getLogger(Main.class);
public static final String BASE_URI = "http://0.0.0.0:8080";
public static HttpServer startServer()
{
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setBasePath("/");
beanConfig.setResourcePackage("your packages");
beanConfig.setScan(true);
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), new ResourceConfig());
// Initialize and register Jersey Servlet
WebappContext context = new WebappContext("WebappContext", "");
ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class);
registration.setInitParameter("javax.ws.rs.Application", MyApplication.class.getName());
registration.addMapping("/*");
context.deploy(httpServer);
return httpServer;
}
public static void main(String[] args) throws Exception
{
startServer();
}
}
这篇关于使swagger-core 1.5与Jersey和Grizzly一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!