首页 文章

如何在Spring中使用Swagger和Jersey?

提问于
浏览
5

我试图在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 .
enter image description here

我试着追随这个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配置

1 回答

  • 4

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

    它似乎也在使用 SpringFox 生成的Swagger bean而不是 BeanConfig .

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

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

    您可能还需要将 BeanConfig 中的 resource package 更改为您的应用程序库包 . 目前,您有:

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

    这是一个工作example与Spring Boot,Jersey和Swagger . 在此示例中,资源包设置为应用程序基础包:

    beanConfig.setResourcePackage("com.basaki");
    

相关问题