我有一个在嵌入式Grizzly服务器上运行的Jersey2应用程序 - 与此示例相同的设置: 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定义,我想升级到那个版本。
遵循本网站的Swagger设置说明: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 我发现Swagger不再工作,因为它需要注入一个ServletContext实例, 和@Context注释的ServletContext字段没有被注入到我的项目中(它们显示为空值)。
所以我的实际问题是:jersey-container-grizzly2-servlet是否支持ServletContext? 有没有什么方法可以通过改变我的项目的配置来注入ServletContext? 或者我应该研究如何整合不需要ServletContext的swagger-core 1.5?
答案 0 :(得分:0)
这就是我的工作方式:
将这些依赖项添加到您的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和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:
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();
}
}