CXF Swagger2Feature
The CXF Swagger2Feature
我有一个使用 Apache CXF 和嵌入式 Jetty 的 REST 服务。我想使用 The CXF Swagger2Feature 来实现 swagger 文档,但是 swagger UI returns “No operations defined in spec!”
我试过使用注释和属性,但无论我做什么都显示相同的输出。
这是我的代码:
public class AppConfig {
@Bean( destroyMethod = "shutdown" )
public SpringBus cxf() {
return new SpringBus();
}
@Bean
public Server jaxRsServer() {
Swagger2Feature feature = new Swagger2Feature();
feature.setUsePathBasedConfig(true);
feature.setResourcePackage("com.phoenixbv.rs");
feature.setResourcePackage(MaterialsRestService.class.getPackage().getName());
feature.setScanAllResources(true);
JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint( jaxRsApiApplication(), JAXRSServerFactoryBean.class );
factory.setServiceBeans( Arrays.< Object >asList(baseRestService(), peopleRestService(),materialsRestService(),batchRestService(),billingRestService(),locationRestService() ) );
factory.setAddress(factory.getAddress() );
factory.setProviders( Arrays.< Object >asList( jsonProvider(), authenticationService() ) );
factory.getFeatures().add(feature);
return factory.create();
}
还有一项服务class。
@Path( "/materials" )
@Api(value="Materials", description = "Endpoint for Material specific operations")
public class MaterialsRestService extends BaseRestService {
@Inject
private GenericUserRightsUtil genericUserRightsUtil;
@Inject
private PxMaterialService materialsService;
@Produces( { "application/json" } )
@GET
@ApiOperation(value = "GetAllMaterials", notes = "Returns a list of materials", response = MaterialsRestService.class)
@ApiResponse(code = 200, message = "Successful retrieval of all materials", response = MaterialsRestService.class)
public Response getMaterialList(@Context SecurityContext securityContext) {
Principal principal = securityContext.getUserPrincipal();
String empid = principal.getName();
Employee emp = getLoggedInUser(empid);
if((!genericUserRightsUtil.get(emp, Material.class).hasRights(UserRights.Dump))){
return null;
}
return Response.status(200).entity(materialsService.getMaterialList()).build();
}
在 swagger-ui 和 service-description-swagger 的依赖项中更改工件的版本解决了我的问题
目前我正在使用:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-swagger</artifactId>
<version>3.1.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/swagger-ui -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.1.5</version>
</dependency>
我有一个使用 Apache CXF 和嵌入式 Jetty 的 REST 服务。我想使用 The CXF Swagger2Feature 来实现 swagger 文档,但是 swagger UI returns “No operations defined in spec!”
我试过使用注释和属性,但无论我做什么都显示相同的输出。
这是我的代码:
public class AppConfig {
@Bean( destroyMethod = "shutdown" )
public SpringBus cxf() {
return new SpringBus();
}
@Bean
public Server jaxRsServer() {
Swagger2Feature feature = new Swagger2Feature();
feature.setUsePathBasedConfig(true);
feature.setResourcePackage("com.phoenixbv.rs");
feature.setResourcePackage(MaterialsRestService.class.getPackage().getName());
feature.setScanAllResources(true);
JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint( jaxRsApiApplication(), JAXRSServerFactoryBean.class );
factory.setServiceBeans( Arrays.< Object >asList(baseRestService(), peopleRestService(),materialsRestService(),batchRestService(),billingRestService(),locationRestService() ) );
factory.setAddress(factory.getAddress() );
factory.setProviders( Arrays.< Object >asList( jsonProvider(), authenticationService() ) );
factory.getFeatures().add(feature);
return factory.create();
}
还有一项服务class。
@Path( "/materials" )
@Api(value="Materials", description = "Endpoint for Material specific operations")
public class MaterialsRestService extends BaseRestService {
@Inject
private GenericUserRightsUtil genericUserRightsUtil;
@Inject
private PxMaterialService materialsService;
@Produces( { "application/json" } )
@GET
@ApiOperation(value = "GetAllMaterials", notes = "Returns a list of materials", response = MaterialsRestService.class)
@ApiResponse(code = 200, message = "Successful retrieval of all materials", response = MaterialsRestService.class)
public Response getMaterialList(@Context SecurityContext securityContext) {
Principal principal = securityContext.getUserPrincipal();
String empid = principal.getName();
Employee emp = getLoggedInUser(empid);
if((!genericUserRightsUtil.get(emp, Material.class).hasRights(UserRights.Dump))){
return null;
}
return Response.status(200).entity(materialsService.getMaterialList()).build();
}
在 swagger-ui 和 service-description-swagger 的依赖项中更改工件的版本解决了我的问题
目前我正在使用:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-swagger</artifactId>
<version>3.1.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/swagger-ui -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.1.5</version>
</dependency>