是否有可用于为Swagger配置spring boot + Jersey + JaxRs应用程序的示例?
参考this帖子,我的应用程序中有以下代码,无法显示swagger UI,也无法使用
列出资源http://localhost:8080/api/root/{funcId}/entities
ApplicationInitializer类:(Groovy)
@Configuration
@EnableAutoConfiguration
class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run( Application.class, args)
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources( Application, "classpath:/META-INF/com/company/automation/functionality/bootstrap.xml");
}
@Bean
ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new SpringServlet());
Map params = ["com.sun.jersey.config.property.packages":"com.company.automation.functionality.impl;com.wordnik.swagger.jersey.listing"]
registration.setInitParameters(params)
return registration;
}
@Bean
ServletRegistrationBean jerseyJaxrsConfig() {
ServletRegistrationBean registration = new ServletRegistrationBean(new JerseyJaxrsConfig());
Map params = ["swagger.api.basepath":"http://localhost:8080/api", "api.version":"1.0"]
registration.setInitParameters(params)
return registration;
}
}
资源:
@Component
@Path('root/{funcId}/entities')
@Api (value = "root/{funcId}/entities", description = "Operations about entity Details")
@CompileStatic
class EntityDetailsResource {
..
}
[编辑]。我正在使用泽西岛1.16,因应用程序影响而无法使用泽西2
答案 0 :(得分:2)
我能够让它工作,但是我不确定在Jersey 1.x中Spring支持有多好,所以我用2.7做了(你发布的链接是针对JAX-RS 1.x的) )。这很有效:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@Path("/")
@Api(value = "home", description = "Demo API")
public class Application extends ResourceConfig implements CommandLineRunner {
@GET
@ApiOperation(value = "Get Greeting", notes = "Returns greeting")
public String home() {
return "Hello";
}
@Override
public void run(String... args) throws Exception {
SwaggerConfig config = ConfigFactory.config();
config.setBasePath("http://localhost:8080/");
config.setApiVersion("1.0.0");
ScannerFactory.setScanner(new DefaultJaxrsScanner());
ClassReaders.setReader(new JerseyApiReader());
}
public Application() {
register(Application.class);
packages("com.wordnik.swagger.jersey.listing");
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
它使用spring-boot-jersey来创建Jersey servlet,但如果你想坚持使用vanilla Boot,你可以使用ServletRegistration
@Bean
轻松完成。
我必须固定泽西岛版本并添加一些排除。以下是我的堕落:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
<version>1.3.2</version>
<exclusions>
<exclusion>
<artifactId>jsr311-api</artifactId>
<groupId>javax.ws.rs</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-jersey</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
答案 1 :(得分:2)
以下是使用Jersey 2的示例Spring Boot应用程序:http://raibledesigns.com/rd/entry/a_webapp_makeover_with_spring