如何将JerseyJaxrsConfig类作为servlet添加到grizzly中

时间:2013-09-09 15:43:37

标签: java grizzly swagger

我正在努力实施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();
    }
}

1 个答案:

答案 0 :(得分:2)

你的方式错了。 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");

它基本上做同样的事情;我也想定制其他更具特色的东西,所以我一直使用JerseyJaxrsConfig类,所以我使用了第一个咒语,但你的里程可能会有所不同。

希望有所帮助!