【问题标题】:Can't get json from Swagger + Jersey无法从 Swagger + Jersey 获取 json
【发布时间】:2015-05-15 11:21:15
【问题描述】:

我有基于 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;
}
}

商店资源

@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,但没有得到任何结果。
我没有在我的应用程序 (/tomcat8/webapps/app) 的根目录中找到 swagger.json

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

【问题讨论】:

  • 是的。它不起作用
  • 这没有帮助。你得到什么错误? 404还是别的什么?您可以访问自己的 API 吗?
  • 我连接到 server.chrome 时出错:“此网页不可用”。是的,我可以调用 API 并且应用程序工作正常。但是应用部署在 8080 端口上。

标签: java api rest jersey swagger


【解决方案1】:

我没有正确构建网址。

正确:

http://{host}:{port}/{应用程序的上下文根}/{@ApplicationPath的路径}/swagger.json

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

感谢Ron

【讨论】:

    【解决方案2】:

    添加相对路径对我有用(这是使用 .netcore 1.1)

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

    【讨论】:

      猜你喜欢
      • 2016-04-21
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 2014-06-08
      • 2015-09-17
      相关资源
      最近更新 更多