我正在努力实现Swagger以生成 API 文档。我在这里遵循配置指南:https ://github.com/wordnik/swagger-core/wiki/Java-JAXRS-Quickstart但它都是基于 XML 的,当我尝试做我认为在运行时等效的事情时配置灰熊抱怨 - Cannot resolve method 'addServlet(java.lang.String, com.wordnik.swagger.jersey.config.JerseyJaxrsConfig)'
。
似乎是,JerseyJaxrsConfig extends HttpServlet
而不是Servlet
. 关于我能做什么的任何建议?
public class Main {
public static final URI BASE_URI = getBaseURI();
public static final String API_VERSION = "0.1.0";
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(9998).build();
}
protected static HttpServer startServer() throws IOException {
ResourceConfig rc = new PackagesResourceConfig("com.my.package.api.resources", "com.wordnik.swagger.jersey.listing");
rc.getFeatures()
.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
}
public static void main(String[] args) throws IOException {
//System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%n");
System.setProperty("jsse.enableSNIExtension", "false"); //avoid unrecognized_name during SSL handshake with deconet
AnnotationConfigApplicationContext annotationCtx = new AnnotationConfigApplicationContext(Config.class);
//add API documentation
WebappContext ctx = new WebappContext("Documentation", "/docs");
ServletRegistration swaggerServletRegistration = ctx.addServlet("JerseyJaxrsConfig", new com.wordnik.swagger.jersey.config.JerseyJaxrsConfig());
swaggerServletRegistration.setInitParameter("api.version", API_VERSION);
swaggerServletRegistration.setInitParameter("swagger.api.basepath", BASE_URI.toString());
swaggerServletRegistration.setLoadOnStartup(2);
swaggerServletRegistration.addMapping("/*");
HttpServer httpServer = startServer();
System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nHit enter to stop it...", BASE_URI, BASE_URI));
System.in.read();
httpServer.stop();
}
}