A
A
Alexiuscrow2015-05-15 15:01:25
Java
Alexiuscrow, 2015-05-15 15:01:25

Why can't I generate json from my service api via swagger?

I have a RESTful service based on Jersey 1.18.1 and I want to expose my API via Swagger .
First I have to get JSON. Swagger allows you to customize the configuration in several ways, and I decided to use a subclass of the Application class for configuration. As a result, I was not able to get JSON, which I could then use for swagger-ui .
Subclass of class "Application"

@ApplicationPath("api/v1")
public class DiscountsApp extends Application{

public DiscountsApp() {
    BeanConfig beanConfig = new BeanConfig();
    beanConfig.setVersion("1.0.2");
    beanConfig.setSchemes(new String[]{"http"});
    beanConfig.setHost("localhost:8002");
    beanConfig.setBasePath("swaggerapi");
    beanConfig.setResourcePackage("alexiuscrow.diploma.endpoints");
    beanConfig.setScan(true);
}

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> resources = new HashSet();
    resources.add(ShopsResources.class);
    //...       
    resources.add(com.wordnik.swagger.jaxrs.listing.ApiListingResource.class);
    resources.add(com.wordnik.swagger.jaxrs.listing.SwaggerSerializers.class);
    return resources;
}
}

ShopsResources
@Path("/shops")
@Api(value="/shops", description="Shops")
public class ShopsResources {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "List shops", httpMethod = "GET", 
        notes = "List nearest or locality shops",
        response = Shops.class, responseContainer = "List")
    public String getShops(
            @ApiParam( value = "Radius", required = false)
            @QueryParam("radius") String radiusParam, 
            @ApiParam( value = "Latitude", required = true)
            @QueryParam("lat") String latParam,
            @ApiParam( value = "Longitude", required = true)
            @QueryParam("lng") String lngParam) throws SQLException{
                //The list of Shops objects is serialized to string 
                //using the custom GSON serializer and I know
                //that there is the better method of the solution of this task.
            }
    }
}

Some dependencies from the pom.xml file
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>jsr311-api</artifactId>
    <version>1.1.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.18.1</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.18.1</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-bundle</artifactId>
    <version>1.18.1</version>
</dependency>
<dependency>
    <groupId>com.wordnik</groupId>
    <artifactId>swagger-jersey-jaxrs</artifactId>
    <version>1.5.1-M2</version>
</dependency>

After deploying the application to Tomcat, I tried to navigate to localhost:8002/swaggerapi but did not get the desired result. I didn't find swagger.json in the app root (/tomcat8/webapps/app).
What could be the problem?
How can I get JSON from my application's API?
Documentation: Swagger Core Jersey 1.X Project Setup 1.5 .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bromzh, 2015-05-15
@Alexiuscrow

UPD2
Yes, exactly, I didn’t notice about the constructor, sorry.
I created a simple project, everything worked out for me. The API address of the swager itself is of this kind:
<Tomcat host>/<Path to the application, i.e. ApplicationContext>/<Value of @ApplicationPath or whatever is in web.xml>/swagger.json
For me it is localhost:8080/api/swagger.json
You will probably have localhost:8080/api/v1/swagger.json
Demo project at github .
PS To use swagger-ui, you still need to configure CORS in Tomcat. Add it

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

to the global web.xml (%TOMCAT_HOME%/conf/web.xml).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question