簡體   English   中英

無法從Swagger + Jersey獲得json

[英]Can't get json from Swagger + Jersey

我有基於Jersey 1.18.1的RESTful服務,我想通過Swagger顯示我的API。
首先,我必須得到JSON。 我讀了這個指令: Swagger Core Jersey 1.X Project Setup 1.5 Swagger允許設置不同的配置方法,我決定使用自定義的Application子類。 我一步一步地做了所有事情,但我不能得到我必須用於swagger-ui的 JSON。

我做了什么:
我的自定義應用

@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.
            }
    }
}

來自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>

在將應用程序部署到Tomcat之后,我嘗試獲取http:// localhost:8002 / swaggerapi,但我沒有結果。
我沒有在我的應用程序的根目錄中找到swagger.json/tomcat8/webapps/app )。

怎么了?
如何使用我的API獲取JSON?

我沒有正確構建網址。

正確:

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

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

Ron來說

添加一個為我工作的相對路徑(這是使用.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