這是我如何得到它的工作:
這些依賴添加到你的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;
}
}
初始化灰熊,新澤西州和揚鞭:
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();
}
}