无法从 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 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.
我做了什么:
我的自定义应用程序
@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?
我没有正确构建 url。
正确:
http://{主机}:{端口}/{应用程序的上下文根}/{@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");
});
我有基于 Jersey 1.18.1 的 RESTful 服务,我想通过 Swagger 显示我的 API。
首先我必须得到JSON。我读了这条指令: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.
我做了什么:
我的自定义应用程序
@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?
我没有正确构建 url。
正确:
http://{主机}:{端口}/{应用程序的上下文根}/{@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");
});