Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
youkuan opened this issue Jun 2, 2016 · 5 comments
Closed

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

youkuan opened this issue Jun 2, 2016 · 5 comments

Comments

@youkuan
Copy link

youkuan commented Jun 2, 2016

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中使用Swagger2构建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.

@fehguy
Copy link
Contributor

fehguy commented Jun 4, 2016

Take a look at this tutorial about SpringBoot + Swagger:

http://heidloff.net/article/usage-of-swagger-2-0-in-spring-boot-applications-to-document-apis/

@fehguy fehguy closed this as completed Jun 4, 2016
@youkuan
Copy link
Author

youkuan commented Jun 6, 2016

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

I want Spring Boot + Jersey +swagger.

@mikek33
Copy link

mikek33 commented Jul 15, 2016

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));

@JaviRpo
Copy link

JaviRpo commented Apr 19, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants