Search code examples
restjax-rscxfswaggerswagger-ui

The CXF Swagger2Feature


I have a REST service that uses Apache CXF and Embedded Jetty. I want to use The CXF Swagger2Feature to implement swagger documentation, but the swagger UI returns " No operations defined in spec!"

I've tried to play around with the annotations and properties but whatever I do it shows the same output.

Here is my code:

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

And a service 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();
}

Solution

  • Changing the version of the artifact in the dependency for both the swagger-ui and the service-description-swagger solved the problem for me

    Currently I am using:

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