【发布时间】:2015-03-26 14:48:50
【问题描述】:
我对 JAVA 很陌生,更具体地说,我对 JAVA 中基于 REST 的服务很陌生。
我将 Grizzly 用作嵌入式 Web 服务器,提供 Jersey REST API。这一切都很好,但是当我尝试添加 Swagger 来记录 API 时,它不起作用。
这是我的 POM(使用 maven)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>swagger_test</groupId>
<artifactId>swagger_grizzly_test</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- bring in all the jersey dependencies we need, from the same version -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.13</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- the web server -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<!-- json serializer -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.10.1</version>
</dependency>
<!-- jersey for API documentation -->
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey-jaxrs_2.10</artifactId>
<version>1.3.12</version>
</dependency>
</dependencies>
</project>
这是我的主要功能,启动服务器。请注意,我的“浏览”资源位于“资源”包下。
public class Main
{
public static void main(String [ ] args)
{
String restUrl = "http://localhost:8080";
// Grizzly makes you add the resources you want to expose
final ResourceConfig rc = new ResourceConfig().packages ("resources", "com.wordnik.swagger.jersey.listing");
HttpServer server = null;
try
{
server = GrizzlyHttpServerFactory.createHttpServer(URI.create (restUrl), rc);
server.start();
System.out.println("Started...");
}
catch (Exception e)
{
System.out.println("Failed to start (" + e.toString () + ")");
}
// Wait for the user to close out of the app
try{System.in.read();} catch (IOException e) {}
if(server != null)
{
server.shutdownNow ();
}
}
}
最后,这是我唯一的资源。
@Path("browse")
@Api(value = "/browse", description = "Browse tags")
public class Browse
{
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Browse for tags", notes = "Returns all tags in a flat list")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 500, message = "Something wrong in Server")})
public String browse ()
{
return "Hello World";
}
}
如果我去http://localhost:8080/api-docs 我会得到...
{
apiVersion: "1.0.0",
swaggerVersion: "1.2"
}
请注意,没有列出任何 API。我已经学习了许多教程,但我没有(直接)使用 servlet,所以我认为这有点不同?
任何帮助都会很棒!
【问题讨论】:
-
我们的google群里有几种解决方案。我建议先看看那里。
-
你能发个链接吗?哪个组?我在这上面花了几个小时,并进行了大量的谷歌搜索。
-
groups.google.com/forum/#!forum/swagger-swaggersocket - 我们的所有支持信息也可以在我们的网站上找到 - swagger.io