简体   繁体   English

无法从Swagger + Jersey获得json

[英]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 . 我有基于Jersey 1.18.1的RESTful服务,我想通过Swagger显示我的API。
Firstly I have to get JSON. 首先,我必须得到JSON。 I read this instruction: Swagger Core Jersey 1.X Project Setup 1.5 . 我读了这个指令: 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. Swagger允许设置不同的配置方法,我决定使用自定义的Application子类。 I did everything step by step but I can't get JSON which I have to use for swagger-ui . 我一步一步地做了所有事情,但我不能得到我必须用于swagger-ui的 JSON。

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 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 来自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. 在将应用程序部署到Tomcat之后,我尝试获取http:// localhost:8002 / swaggerapi,但我没有结果。
I didn't find the swagger.json in root of my application ( /tomcat8/webapps/app ). 我没有在我的应用程序的根目录中找到swagger.json/tomcat8/webapps/app )。

What's wrong? 怎么了?
How can I get JSON with my API? 如何使用我的API获取JSON?

I did not correctly build the url. 我没有正确构建网址。

Correct: 正确:

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

In my case: http://localhost:8080/app/api/v1/swagger.json 就我而言: http:// localhost:8080 / app / api / v1 / swagger.json

Thx to Ron . Ron来说

adding a relative path worked for me (this is using .netcore 1.1) 添加一个为我工作的相对路径(这是使用.netcore 1.1)

app.UseSwaggerUI(s => {
    s.RoutePrefix = "help";
    s.SwaggerEndpoint("../swagger/v1/swagger.json", "MySite");
    s.InjectStylesheet("../css/swagger.min.css");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM