Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Swagger with Jersey in Spring?

Tags:

I am trying to use Swagger (2.6) with Jersey (1.5) in SpringBoot (1.5.8)

My calls to my public API work fine http://localhost:57116/rest/customer/list

When I load up the http://localhost:57116/swagger-ui.html site, it displays Swagger with a lot of default APIs, but it does not display my API. enter image description here

I tried following this Config Swagger-ui with Jersey but it still does not come back.

This is my 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);
}
}

This is my application class

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

This is my endpoint

@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"};
    }
}

When I try a similar config using Spring RestController, it works fine, but with Jersey I don't see my public API. It seems to be ignoring my Swagger configuration

like image 690
Victor Grazi Avatar asked Nov 25 '17 23:11

Victor Grazi


People also ask

How do I add Swagger to Jersey project?

Go to https://github.com/swagger-api/swagger-ui/ and download the repository. Then copy all the contents of the dist folder and paste them into the web folder of the project on IntelliJ.

How do I add Swagger to an existing Spring project?

To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file. For Gradle users, add the following dependencies in your build. gradle file. Now, add the @EnableSwagger2 annotation in your main Spring Boot application.


1 Answers

Are you using org.springframework.boot:spring-boot-starter-jersey dependency or org.springframework.boot:spring-boot-starter-web in your pom.xml?

It also seems you are using the Swagger bean generated by SpringFox and not by your BeanConfig.

In order for Swagger to work, you should rely on spring-boot-starter-jersey and remove all SpringFox dependencies including @EnableSwagger2 annotation.

The swagger.json in a Jersey application is generated by the io.swagger:swagger-jersey2-jaxrs library. As for the Swagger UI, you can use vanilla Swagger UI static resources.

You may also need to change the resource package in your BeanConfig to your application base package. Currently, you have:

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

Here is a working example with Spring Boot, Jersey, and Swagger. In this example, the resource package is set to the application base package:

beanConfig.setResourcePackage("com.basaki");
like image 190
Indra Basak Avatar answered Sep 20 '22 13:09

Indra Basak