我有一个带有Jaxrs + Swagger的嵌入式Jetty应用程序(使用Java),可以正常工作(最后)。它在简单的JSON对象上实现基本的REST操作(GET,PUT,POST,DELETE)。
我有几个HTTPServlet可以处理我的一些REST API,并希望添加Swagger支持。我能找到的设置Swagger + Servlet的唯一例子是使用web.xml进行配置。
不幸的是,我的应用程序不使用web.xml,它使用Java配置所有内容。
1)Swagger是否可以同时支持Jaxrs和Servlets? (它们是互斥的吗?)
2)如果可能,如何配置Swagger以支持Java中的Servlet?
这是我带注释的API:
@Api(value = "myService",
description = "My Config Service API",
produces = "application/json",
consumes = "application/json")
@Path("/env")
@Produces("application/json")
@Consumes("application/json")
public class MyServiceResource
{
@GET
@Path("/thing/{thingName}")
@ApiOperation(value = "Gets Thing", response = Thing.class)
public Response getThing(
@PathParam("thingName") String thingName) throws IOException
{
return blah blah blah
}
blah blah blah
}
这是Swagger配置文件:
@WebServlet(name = "SwaggerJaxrsConfig", loadOnStartup = 1)
public class SwaggerJaxrsConfig extends HttpServlet {
@Override
public void init(ServletConfig servletConfig) {
super.init(servletConfig);
SwaggerConfig swaggerConfig = new SwaggerConfig();
ConfigFactory.setConfig(swaggerConfig);
swaggerConfig.setBasePath("http://localhost:8002");
swaggerConfig.setApiVersion("1.0.0");
ScannerFactory.setScanner(new DefaultJaxrsScanner());
ClassReaders.setReader(new DefaultJaxrsApiReader());
}
}
这是一些文件,可以进行更多的Swagger设置:
public class ServiceApplication extends Application
{
HashSet<Object> singletons = new HashSet<Object>();
public ServiceApplication()
{
singletons.add(new MyServiceResource());
}
@Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
resources.add(ApiDeclarationProvider.class);
resources.add(ApiListingResourceJSON.class);
resources.add(ResourceListingProvider.class);
resources.add(MyServiceResource.class);
return resources;
}
@Override
public Set<Object> getSingletons()
{
return singletons;
}
}
以下是将上述所有内容联系在一起的服务器代码:
public class MyService {
private Server server;
public MyService() {
}
public void start() throws Exception {
server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8002);
server.addConnector(connector);
// Setup web handler
final ResourceHandler webResourceHandler = new ResourceHandler();
webResourceHandler.setDirectoriesListed(false);
webResourceHandler.setWelcomeFiles(new String[]{"index.html"});
webResourceHandler.setResourceBase("webapp");
// Create servlet context
final ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContext.addEventListener(new DefaultResteasyBootstrap());
servletContext.setContextPath("/");
// Setup Swagger handlers
servletContext.addServlet(new ServletHolder(new SwaggerJaxrsConfig()), "");
// Add MyService Servlet
final ServletHolder servletHolder = new ServletHolder(new HttpServletDispatcher());
servletHolder.setInitParameter("javax.ws.rs.Application", MyServiceResource.class.getName());
servletContext.addServlet(servletHolder, "/*");
// Add Admin Servlet
final ServletHolder adminServletHolder = new ServletHolder(new HttpServletDispatcher());
servletContext.addServlet(adminServletHolder, "/admin/*");
// Add Admin Servlets
servletContext.addServlet(new ServletHolder(new PingServlet()), "/ping");
servletContext.addServlet(new ServletHolder(new ThreadDumpServlet()), "/threadDump");
final HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{
webResourceHandler, // Web handler
servletContext, // Servlet handler
new DefaultHandler()}); // Default handler returns 404 (NOT FOUND) for anything else
server.setHandler(handlers);
server.start();
server.join();
}
}