I am working on implementing Swagger to generate API documentation. I'm following the configuration guide here: https://github.com/wordnik/swagger-core/wiki/Java-JAXRS-Quickstart but it's all XML based, and when I try to do what I believe in the equivalent in runtime configuration Grizzly complains - Cannot resolve method 'addServlet(java.lang.String, com.wordnik.swagger.jersey.config.JerseyJaxrsConfig)'.

It seems to be that JerseyJaxrsConfig extends HttpServlet instead of Servlet. Any suggestions on what I can do?

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();
    }
}
有帮助吗?

解决方案

You're going about it the wrong way. JerseyJaxrsConfig is a hack to pass in config params to Swagger, statically. You can just do something like this in startServer()

ServletConfig sc = new MyServletConfig();
JerseyJaxrsConfig jjc = new JerseyJaxrsConfig();
jjc.init(sc);

return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);

Here's a sample MyServletConfig class (I use an inner class on 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());
    }
}

I frankly have no clue about most of what you are doing in main(String[]), but if you were trying to mock a web context to pass on those values to Swagger, drop the code. Mine looks like this:

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();
}

All that being said, this is an alternative way of doing it:

ConfigFactory.config().setBasePath("http://localhost:8080/content-store");

It basically does the same thing; I also wanted to customize other things that are more Jersey specific so I kept using the JerseyJaxrsConfig class, so I used the first incantation, but your mileage may vary.

Hope that helps!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top