1

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

1 回答 1

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 类,所以我使用了第一个咒语,但你的里程可能会有所不同。

希望有帮助!

于 2013-09-14T17:47:26.697 回答