如何在Spring中使用Swagger和Jersey?

Vic*_*azi 5 jersey swagger-ui

我试图在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,但它仍然没有回来.

这是我的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);
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序类

@SpringBootApplication
@EnableDiscoveryClient
@EnableSwagger2
public class CustomerApplication {...}
Run Code Online (Sandbox Code Playgroud)

这是我的终点

@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"};
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Ind*_*sak 6

您在使用org.springframework.boot:spring-boot-starter-jersey依赖项还是org.springframework.boot:spring-boot-starter-web在您的pom.xml?

看来您正在使用由SpringFox而不是由您生成的 Swagger bean BeanConfig。

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

swagger.jsonJersey 应用程序中的 是由库生成的io.swagger:swagger-jersey2-jaxrs。对于 Swagger UI,您可以使用普通 Swagger UI静态资源。

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

beanConfig.setResourcePackage("io.swagger.resources");
Run Code Online (Sandbox Code Playgroud)

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

beanConfig.setResourcePackage("com.basaki");
Run Code Online (Sandbox Code Playgroud)