Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get json from Swagger + Jersey

I have RESTful service based on Jersey 1.18.1 and I want to show my API via Swagger.
Firstly I have to get JSON. I read this instruction: Swagger Core Jersey 1.X Project Setup 1.5. Swagger allows to set up a configuration different methods and I decided to use custom Application subclass. I did everything step by step but I can't get JSON which I have to use for swagger-ui.

What I did:
My custom 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 pom.xml

<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 deploy application to Tomcat I tried to get http://localhost:8002/swaggerapi but I've got no result.
I didn't find the swagger.json in root of my application (/tomcat8/webapps/app).

What's wrong?
How can I get JSON with my API?

like image 783
Alexiuscrow Avatar asked Oct 31 '22 04:10

Alexiuscrow


1 Answers

I did not correctly build the url.

Correct:

http://{host}:{port}/{context root of application}/{path from @ApplicationPath}/swagger.json

In my case: http://localhost:8080/app/api/v1/swagger.json

Thx to Ron.

like image 53
Alexiuscrow Avatar answered Nov 13 '22 02:11

Alexiuscrow