Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger REST API annotation not working on interface but working on implementation class

This is my Interface ClassA .java

@Path("/"+Paths.STORIES)
@ApiModel(value = "Name API")
@Api(value = "/stories", description = "Name API")
public interface ClassA {
    @GET
    @Path("/"+Paths.STORYID)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Fetch Story by ID", notes = "More notes about this method")
    @ApiResponses(value = {
              @ApiResponse(code = 400, message = "Invalid ID supplied"),
              @ApiResponse(code = 200, message = "Invalid ID supplied"),
            })
    public Response getNameFromID(@PathParam("nameId") String nameId);
}

this is my implementation class.

@Singleton
@Component
public class ClassB implements ClassA,InitializingBean{
    @Override
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Response getNameFromID(final String nameId) {
        Map NameResponse = new HashMap<String,Object>();
        NameResponse.put("repsonseCode", "200");
        NameResponse.put("errorCode", "");
        return Response.status(200).entity(NameResponse).build();
    }
}

Application-context.xml entry

<bean id="swaggerConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
        <property name="resourcePackage" value="com.razak.sample" />
        <property name="version" value="1.0.0" />
        <property name="basePath" value="http://localhost:8080/api" />
        <property name="title" value="Petstore sample app" />
        <property name="description" value="This is a app." />
        <property name="contact" value="[email protected]" />
        <property name="license" value="Apache 2.0 License" />
        <property name="licenseUrl"
            value="http://www.apache.org/licenses/LICENSE-2.0.html" />
        <property name="scan" value="true" />
    </bean>

web.xml entry

    <param-name>swagger.version</param-name>

    <param-value>1.1</param-value>

</init-param>

<init-param>

    <param-name>swagger.api.basepath</param-name>

    <param-value>http://localhost:8080/api</param-value>

</init-param>

<init-param>

    <param-name>swagger.security.filter</param-name>

    <param-value>com.wordnik.swagger.sample.util.ApiAuthorizationFilterImpl</param-value>

</init-param>

@Api(value = "/stories", description = "Story API") at class level and 
    @GET
    @Path("/"+Paths.STORYID)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Fetch Story by ID", notes = "More notes about this method")
    @ApiResponses(value = {
              @ApiResponse(code = 400, message = "Invalid ID supplied"),
              @ApiResponse(code = 200, message = "Invalid ID supplied"),
            })

When i moved these entry from interface to Implementation class.i m able to access rest endpoints in swagger.swagger is working.but when i placed that annotation in interface itself.it is not working.

like image 952
Abdul Razak AK Avatar asked May 29 '14 11:05

Abdul Razak AK


People also ask

What is @API annotation in Swagger?

The Springfox library provides @Api annotation to configure a class as a Swagger resource. Previously, the @Api annotation provided a description attribute to customize the API documentation: @Api(value = "", description = "") However, as mentioned earlier, the description attribute is deprecated.

What does @API annotation do?

The annotation @Api configures the whole API, and applies to all public methods of a class unless overridden by @ApiMethod . Important: If you implement your API from multiple classes, see Multiclass APIs. To override a given @Api annotation for a specific class within an API, see @ApiClass and @ApiReference .

What is @schema annotation in Swagger?

The annotation may be used to define a Schema for a set of elements of the OpenAPI spec, and/or to define additional properties for the schema. It is applicable e.g. to parameters, schema classes (aka "models"), properties of such models, request and response content, header.


1 Answers

This is an open issue: https://github.com/wordnik/swagger-core/issues/562

There is also an open issue to document how to override the default scanner: https://github.com/wordnik/swagger-core/issues/513

I have not tried this out yet, but I think the solution to both the issues is found in this solution: https://github.com/wordnik/swagger-core/wiki/Java-Setup-for-Spring---Jersey---JAX-RS

The trick is to get Swagger to use the ReflectiveJaxrsScanner instead of the DefaultJaxrsScanner.

like image 64
David Fleeman Avatar answered Oct 07 '22 12:10

David Fleeman