首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在jersey中使用swagger和ResourceConfig?

如何在jersey中使用swagger和ResourceConfig?
EN

Stack Overflow用户
提问于 2016-11-08 14:12:02
回答 1查看 3.4K关注 0票数 3

我正在开发一个REST api,并尝试使用Swagger和Jersey来做到这一点。

我正在遵循这里给出的指南:- https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5

我使用ResourceConfig而不是Application类,并相应地修改了指南中的步骤。我正在grizzly上部署API。

下面是主文件:-

代码语言:javascript
运行
复制
package com.example;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import java.io.IOException;
import java.net.URI;

import io.swagger.jaxrs.config.*;

/**
 * Main class.
 *
 */
public class Main {
    // Base URI the Grizzly HTTP server will listen on
    public static final String BASE_URI = "http://localhost:8080/";

    /**
     * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
     * @return Grizzly HTTP server.
     */
    public static HttpServer startServer() {
        // create a resource config that scans for JAX-RS resources and providers
        // in com.example package
        final ResourceConfig rc = new ResourceConfig().packages("com.example");
        rc.register(io.swagger.jaxrs.listing.ApiListingResource.class);
        rc.register(io.swagger.jaxrs.listing.SwaggerSerializers.class);
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.2");
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost("localhost:8080");
        beanConfig.setBasePath("resources/api");
        beanConfig.setResourcePackage("io.swagger.resources");
        beanConfig.setScan(true);

        // create and start a new instance of grizzly http server
        // exposing the Jersey application at BASE_URI
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    }

    /**
     * Main method.
     * @param args
     * @throws IOException
     */
    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();
    }
}

现在,当我试图通过url http://localhost:8080/swagger.json访问swagger.json时,我得到了一个500,请求失败的错误。

为什么会这样?我如何调试它?

EN

回答 1

Stack Overflow用户

发布于 2017-04-17 05:55:29

看看这个项目:

https://github.com/sofien-hamdi/rest-mongo

我已经使用BeanConfig对其进行了配置。

代码语言:javascript
运行
复制
package com.kt.examples.rs.configuration;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.wadl.internal.WadlResource;
import org.springframework.context.annotation.Configuration;

import com.kt.examples.rs.controller.UserController;

import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;

@Configuration
public class JerseyConfig extends ResourceConfig {

  public JerseyConfig() {
    this.registerEndpoints();
    this.configureSwagger();
  }

  private void registerEndpoints() {
    this.register(UserController.class);
    this.register(WadlResource.class);
    // http://localhost:9090/v1/application.wadl
  }

  private void configureSwagger() {
    this.register(ApiListingResource.class);
    this.register(SwaggerSerializers.class);
    BeanConfig config = new BeanConfig();
    config.setConfigId("spring-boot-jaxrs-swagger");
    config.setTitle("Spring boot jaxrs swagger integration");
    config.setVersion("v1");
    config.setBasePath("/");
    config.setResourcePackage("com.kt.examples.rs");
    config.setPrettyPrint(true);
    config.setScan(true);
    // http://localhost:9090/v1/swagger.json
  }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40480131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档