Spring Boot + Jersery2 + Swagger2 It can't work at the same time
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;
}
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
}
}
@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();
}
@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.
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/
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?
Most helpful comment
but it is Spring Boot + Swagger2.0 , Not have Jersey, Not is Jersey's tage.
I want Spring Boot + Jersey +swagger.