【问题标题】:How do I use Swagger with Jersey in Spring?如何在 Spring 中将 Swagger 与 Jersey 一起使用?
【发布时间】:2017-11-25 23:56:41
【问题描述】:

我正在尝试在 SpringBoot (1.5.8) 中将 Swagger (2.6) 与 Jersey (1.5) 一起使用

我对公共 API 的调用工作正常 http://localhost:57116/rest/customer/list

当我加载http://localhost:57116/swagger-ui.html 站点时,它会显示带有许多默认 API 的 Swagger,但它不会显示我的 API。

我尝试关注这个Config Swagger-ui with Jersey,但它仍然没有回来。

这是我的 JerseyConfig

@Configuration
@ApplicationPath("rest")
public class JerseyConfig extends ResourceConfig {

public JerseyConfig()
{
    register(CustomerImpl.class);
    configureSwagger();
}
public void configureSwagger()
{
    this.register(ApiListingResource.class);
    this.register(SwaggerSerializers.class);
    BeanConfig beanConfig = new BeanConfig();
    beanConfig.setTitle("Swagger sample app");
    beanConfig.setVersion("1.0.0");
    beanConfig.setSchemes(new String[]{"http"});
    beanConfig.setHost("localhost:57116");
    beanConfig.setBasePath("/rest");
    beanConfig.setResourcePackage("com.mypackage");
    beanConfig.setScan(true);
}
}

这是我的应用类

@SpringBootApplication
@EnableDiscoveryClient
@EnableSwagger2
public class CustomerApplication {...}

这是我的终点

@Component
@Path("customer")
@Api(value = "Customer API")
public class CustomerImpl {
    @Path("list")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "list")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Success", response = List.class),
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Forbidden"),
            @ApiResponse(code = 404, message = "Not Found"),
            @ApiResponse(code = 500, message = "Failure")})
    public String[] getList()
    {
        return new String[]{"IBM", "Microsoft", "Victor"};
    }
}

当我使用 Spring RestController 尝试类似的配置时,它工作正常,但在 Jersey 中我看不到我的公共 API。它似乎忽略了我的 Swagger 配置

【问题讨论】:

    标签: jersey swagger-ui


    【解决方案1】:

    你是在pom.xml 中使用org.springframework.boot:spring-boot-starter-jersey 依赖还是org.springframework.boot:spring-boot-starter-web

    您似乎也在使用由SpringFox 而不是您的BeanConfig 生成的Swagger bean。

    为了让 Swagger 工作,您应该依赖 spring-boot-starter-jersey 并删除所有 SpringFox 依赖项,包括 @EnableSwagger2 注释。

    Jersey 应用程序中的swagger.jsonio.swagger:swagger-jersey2-jaxrs 库生成。至于 Swagger UI,可以使用vanilla Swagger UI 静态资源。

    您可能还需要将 BeanConfig 中的 资源包 更改为您的应用程序基础包。目前,您有:

    beanConfig.setResourcePackage("io.swagger.resources");
    

    这是一个使用 Spring Boot、Jersey 和 Swagger 的工作 example。 本例中,资源包设置为应用基础包:

    beanConfig.setResourcePackage("com.basaki");
    

    【讨论】:

    • 你是如何尝试运行的,你遇到了什么类型的错误?
    • 当我删除 springfox 依赖项和@EnableSwagger2 时,每次执行任何操作时都会出现以下对话框:无法推断基本 url。这在使用动态 servlet 注册或 API 位于 API Gateway 后面时很常见。基本 url 是提供所有 swagger 资源的根。例如如果 api 在 example.org/api/v2/api-docs 可用,则基本 url 为 example.org/api。请手动输入位置:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多