我正在尝试使用Jersey和swagger构建Hello World REST服务来生成文档。 我能够成功调用其余方法,但是当我尝试通过调用来访问swagger文档时:
http://localhost:8080/com.swagger.first/rest/api-docs
我得到'404 Not found'。 (我还尝试将.json添加到最后)
我按照this tutorial中的步骤来构建我的服务。
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>com.swagger.first</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.wordnik.swagger.jaxrs.json,
com.swagger.first
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
com.wordnik.swagger.jersey.listing.ApiListingResourceJSON,
com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider,
com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8080/com.swagger.first/rest</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
这是我的pom.xml:
<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>com.swagger.first</groupId>
<artifactId>com.swagger.first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
<version>1.3.12</version>
</dependency>
</dependencies>
最后,这是我的服务类:
package com.swagger.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import io.swagger.annotations.*;
@Api( value="hello", description="HelloWrold service")
@Path("/hello")
public class HelloWorld {
@GET
@Produces(MediaType.TEXT_PLAIN)
@ApiOperation(value="Returns 'Hello World!!' string", notes="No comment")
@ApiResponse(code=200, message="Succeeded")
public String helloWorld(){
return "Hello World!!";
}
@GET
@Produces(MediaType.APPLICATION_XML)
@ApiOperation(value="Returns 'Hello World!!' string as XML", notes="No comment")
@ApiResponses(value = {
@ApiResponse(code=200, message="Succeeded"),
@ApiResponse(code=400, message="Testing Swagger")
})
public String helloWorldXml(){
return "<hello> Hello World!! </hello>";
}
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/echo")
@ApiOperation(value="Returns the query param", notes="No comment")
public String echoStringQueryParam(@ApiParam(name="s", value="any string", required=true) @QueryParam("s") String s){
return s;
}
}
答案 0 :(得分:1)
虽然这个解决方案来得很晚,但我希望它能派上用场找到解决这个问题的人。
您引用的Jersey / Swagger教程未提及Swagger的用户界面未包含在maven安装中。它的核心是生成swagger.json文件,该文件已准备好被Swagger UI使用
如果您要查找此处显示的API文档:http://petstore.swagger.io/,
然后,您需要安装以下npm包并将其内容复制到Java项目中的webapp文件夹。 https://www.npmjs.com/package/swagger-ui-dist
您的基本项目结构应如下所示:
您还需要编辑index.html文件,以便在那里定义的url变量链接到本地swagger.json文件,而不是默认设置的petstore示例。
就我而言,它看起来像这样:
<script>
window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "http://localhost:8080/simple-service-webapp/api/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
}
部署后,您应该能够在浏览器中访问应用程序的根URL时看到文档
答案 1 :(得分:0)
感谢Alexender分享此重要信息,这是启动https://hub.packtpub.com/restful-java-web-services-swagger/时可以遵循的另一个链接,当您能够获得本教程链接中提到的swagger.json格式的swagger规范后,我猜应该遵循alexender答复中提到的步骤,以获得最终的精美招摇文档。