1

I have developed a RESTful web service in Java and Spring boot using Jax-RS and I would like to document it with Swagger. I have so far successfully managed to map the swagger-ui.html page on http:8080/localhost/<context>/swagger-ui.html. Unfortunately, my RESTful endpoints do not appear anywhere.

What I am using:

pom.xml

 <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

Swagger configuration class

@Configuration
@EnableSwagger2
public class SwaggerConfiguration
{
    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket api()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.nick.java.webservice.services"))
                .paths(PathSelectors.any())
                .build()
                .enable(true)
                .apiInfo(getApiInfo())
                .tags(
                        new Tag("My web service", "Methods for my RESTful service")
                );
    }

    private ApiInfo getApiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("API Documentation")
                .description("API")
                .version("1.0")
                .contact(new Contact("mycompany", "", "nickath@mycompany.com"))
                .build();

        return apiInfo;
    }

an example of the JAX-RS endpoints

package org.nick.java.webservice.services;

@Path("/contextsapi")
@Consumes("application/json")
@Produces("application/json")
@Api(value = "Contexts API", produces = "application/json")
public interface ContextAPI {

    @Path("/contexts/contexts")
    @GET
    @ApiOperation( value = "get contexts",
                   response = List.class)
    List<Context> getContexts();

screenshot of the swagger-ui.html page

screenshot

as you can see, no 'get contexts' method has been generated

Any idea what I am doing wrong?

======= UPDATE - SERVICE IMPLEMENTATION ========

package  org.nick.java.webservice.services.impl;
@Service
@Api(value = "Contexts Api Impl", produces = "application/json", description = "desc")
@Path("/contextsapi")
public class ContextAPIImpl implements ContextAPI {

   @Override
   @GET
   @ApiOperation( value = "get contexts", response = List.class)
   public List<Context> getContexts(){
     //code ommitted
   }
}
NickAth
  • 1,089
  • 1
  • 14
  • 35

3 Answers3

1

Solved

Finally I managed to solve my problem using the Swagger2Feature following the example from here https://code.massoudafrashteh.com/spring-boot-cxf-jaxrs-hibernate-maven-swagger-ui/

Maven dependencies

<cxf.version>3.1.15</cxf.version>
<swagger-ui.version>3.9.2</swagger-ui.version>

 <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>swagger-ui</artifactId>
        <version>${swagger-ui.version}</version>
    </dependency>

CxfConfig.java

@Configuration
public class CxfConfig {

@Autowired
private Bus bus;


@Bean
public Server rxServer(){
    final JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
    endpoint.setProvider(new JacksonJsonProvider());
    endpoint.setBus(bus);
    endpoint.setAddress("/swagger");
    endpoint.setServiceBeans(Arrays.<Object>asList(contextAPI());
    Swagger2Feature swagger2Feature = new Swagger2Feature();
    endpoint.setFeatures(Arrays.asList(swagger2Feature));
    return endpoint.create();
}


@Bean
public ContextAPI contextAPI(){
    return new ContextAPIImpl();
}

Now the swagger documentation is available on http://localhost:8080///swagger/api-docs?url=//swagger/swagger.json

To customize the endpoint's UI check the manual here

NickAth
  • 1,089
  • 1
  • 14
  • 35
0

Swagger suppose not to show documentation for any API client. It will generate documentation for your service if there is any with swagger annotations.

To be confirmed about this, try creating a Spring @service and annotate with swagger annotations. The doc will be generated if every other aspects are taken care of. Since you can see the UI, I would assume the dependencies are right.

The idea here is, your task is to document your service and swagger helps with that. It's not your responsibility to generate/publish documentation for API(s) that your service consumes. Since you don't maintain the service, it doesn't make sense to maintain the documentation as well.

When I used Rest client for the first time, I also got a bit perplexed about this. But if you really think about it, this is expected and makes sense.

shakhawat
  • 2,639
  • 1
  • 20
  • 36
  • hi shakhawat , thanks for your response :) . I tried to do as you said ( annotate my @ service class with swagger annotations, but no doc was generated) Actually I added swagger annotations to my service implementing the getContexts function @ Service @ Api(value = "Contexts API", produces = "application/json", description = "description", hidden = false) public class ContextAPIImpl implements ContextAPI { @ Override @ ApiOperation( value = "get contexts", response = List.class) public List getContexts() { //code } } did I miss something? – NickAth Jan 14 '19 at 16:18
  • can you also post your service implementation here? keep the package name as well. – shakhawat Jan 14 '19 at 16:23
  • 1
    check the updates in the question please, I added them there to be more readable :) – NickAth Jan 14 '19 at 16:27
  • can you remove swagger annotations from the interface and change api selector to `.apis(RequestHandlerSelectors.any()) ` ? – shakhawat Jan 14 '19 at 16:34
  • I did as you said and added .paths(Predicates.not(PathSelectors.regex("/error.*")); to avoid basic-error-controller from being shown, but still no luck :/ – NickAth Jan 14 '19 at 16:46
  • Can you share your project? maybe on github/gitlab? – shakhawat Jan 14 '19 at 16:49
  • sorry for being late, I uploaded on my personal github account a repo with an example of the code since I am not allowed to publish the actual project on public, here it is :) https://github.com/Nickath/swaggerexample the endpoint for swagger is here http://localhost:8080/mycontext/swagger-ui.html#/ and the endpoint of the service is here http://localhost:8080/mycontext/services/jcpabecontextsapi/contexts/contexts – NickAth Jan 14 '19 at 17:27
  • Can you follow this? https://stackoverflow.com/questions/32877898/springfox-not-finding-jax-rs-endpoints You need some additional work to document jersey endpoints by swagger – shakhawat Jan 14 '19 at 18:38
  • Thanks for your time :) I do not use the jersey implementation in my service, I found a solution for the apache cxf (which i use) as you can see in the post – NickAth Jan 16 '19 at 15:56
0

I would suggest to use Swagger 2 i faced the same issue. the issue is with the Docket you have implemented , correct regular expression can help. Example :

@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
@Bean
public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2)  
      .select()                                  
      .apis(RequestHandlerSelectors.any())              
      .paths(PathSelectors.any())                          
      .build();                                           
}
}

You can refer to the link above Setting up Swagger 2 Example The Source code example is also from the above link.

Shubham
  • 997
  • 1
  • 9
  • 15