Swagger-core: Spring Boot + Jersery2 + Swagger2 It can't work at the same time

Created on 2 Jun 2016  路  5Comments  路  Source: swagger-api/swagger-core

Spring Boot + Jersery2 + Swagger2 It can't work at the same time

Spring Boot Application run

BootStartInitialization.class

public static void main(String[] args) {
SpringApplication.run(BootStartInitialization.class, args);
}

@Bean
public ServletRegistrationBean jerseyServlet() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/api/*");
    registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
    return registration;
}

Jersey Configuraction

public class JerseyConfig extends ResourceConfig {

public JerseyConfig() {              

    register(io.swagger.jaxrs.listing.ApiListingResource.class);
    register(io.swagger.jaxrs.listing.SwaggerSerializers.class);          
    register(CrossDomainFilter.class);
    register(JacksonFeature.class);
    register(UserController.class);//this my api
}

}

Swagger2 Configuraction

@Configuration
@EnableSwagger2
public class Swagger2 {

@Bean
public Docket createRestApi(ServletConfig config) {     
    return new Docket(DocumentationType.SWAGGER_2)
            //.pathProvider(pathProvider)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.controller"))
            .paths(PathSelectors.any())
            .build();
}

private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title("Spring Boot涓娇鐢⊿wagger2鏋勫缓RESTful APIs")
            .description("鏂藉伐骞冲彴鍚庡彴API")
            .termsOfServiceUrl("http://www.xxxx.com/")
            .contact("Jonathan")
            .version("1.0")
            .build();
}

API Controller(Resource)

@Component
@Controller
@RequestMapping(value="/accounts")
@Path("/accounts")
@Api(value = "accounts manager")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserController {

/**
 * @Fields log slf4j logger
 */
private static Logger logger = LoggerFactory.getLogger(UserController.class);

@POST
@ApiOperation(value = "Add a new user to the store")
public void create(@ApiParam(value = "Account object that needs to be added to the store", required = true) final String userName) {

}

@Path("{userId}")
@GET
@ApiOperation(value = "Get user's Account by userId")
//@RequestMapping(value={"userId"}, method=RequestMethod.GET)
public String get(@ApiParam(value = "get user's account from db.", required = true) @PathParam("userId") final Integer userId) {
    if (userId != null) {
        return userId.toString();
    } else {
        return "userId is be can't null";
    }
}

}

open http://localhost:8080/swagger-ui.html, no display api docment. on found sub api.
can't display http://localhost:8080/accounts/{userId} api document.

It can't work at the same time

Spring Boot + Jersey + Swagger Not run?

who is this close? why ? this question not solved.

Most helpful comment

but it is Spring Boot + Swagger2.0 , Not have Jersey, Not is Jersey's tage.

I want Spring Boot + Jersey +swagger.

All 5 comments

but it is Spring Boot + Swagger2.0 , Not have Jersey, Not is Jersey's tage.

I want Spring Boot + Jersey +swagger.

I suggest you get your resource from spring context before registering in your Jersey config. If you want to run an aspect you'll need spring to be aware.

WebApplicationContext springFactory = WebApplicationContextUtils.getWebApplicationContext(servletContext);
register(springFactory.getBean(UserController.class));

@youkuan I have the same problem. Did you find a solution?

Was this page helpful?
0 / 5 - 0 ratings