我正在实现Swagger来生成API文档。我在这里遵循配置指南:https://github.com/wordnik/swagger-core/wiki/Java-JAXRS-Quickstart,但它都是基于XML的,当我尝试做我认为与运行时配置等效的操作时,Grizzly抱怨-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();
}
}
最佳答案
您正在以错误的方式进行操作。 JerseyJaxrsConfig是一种将配置参数静态传递给Swagger的黑客。您可以在startServer()中做类似的事情
ServletConfig sc = new MyServletConfig();
JerseyJaxrsConfig jjc = new JerseyJaxrsConfig();
jjc.init(sc);
return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
这是一个示例MyServletConfig类(我在Main上使用一个内部类):
private static class MyServletConfig implements ServletConfig {
private Map<String, String> initParams = new HashMap<>();
private MyServletConfig() {
initParams.put("api.version", "1.0.0");
initParams.put("swagger.api.basepath", "http://localhost:8080/content-store");
}
@Override
public String getServletName() {
return "JaxRS Servlet";
}
@Override
public ServletContext getServletContext() {
return null;
}
@Override
public String getInitParameter(String s) {
return initParams.get(s);
}
@Override
public Enumeration getInitParameterNames() {
return Collections.enumeration(initParams.keySet());
}
}
坦率地说,我对main(String [])中的大部分工作一无所知,但是如果您试图模拟Web上下文以将这些值传递给Swagger,请删除代码。我的看起来像这样:
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}
综上所述,这是另一种实现方式:
ConfigFactory.config().setBasePath("http://localhost:8080/content-store");
它基本上做同样的事情。我还想自定义其他更特定于Jersey的内容,因此我继续使用JerseyJaxrsConfig类,因此我使用了第一个咒语,但是您的里程可能会有所不同。
希望有帮助!