我正在尝试使用Swagger查看API文档中的泽西休息服务方法列表。通过GitHub网站提供的几个示例/示例。但是当我尝试访问上下文根链接时,我仍然无法列出我的服务方法。找不到404服务。
的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.danfoss.des</groupId>
<artifactId>SampleRestProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleRestProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
<!-- <dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.9.1</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
</dependency> -->
</dependencies>
<build>
<finalName>SampleRestProject</finalName>
</build>
的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>io.swagger.jaxrs.listing,com.danfoss.des</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<!-- <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:8081/SampleRestProject/</param-value>
</init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>JerseyJaxrsConfig</servlet-name>
<servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8081/SampleRestProject/rest</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> -->
</web-app>
服务java类:
package com.danfoss.des;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.danfoss.model.Track;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Path("/helloWorld")
@Api(value="helloWorld", description="Sample hello world swagger service")
public class RESTfulHelloWorld
{
@GET
@Produces("text/html")
@Path("/startingPage")
@ApiOperation(value="Starting of the swagger service")
public Response getStartingPage()
{
String output = "Staring method is invoked";
return Response.status(200).entity(output).build();
}
}
我尝试访问的链接以查看列表:http://localhost:8081/SampleRestProject/api-docs
有人可以帮我找出到底出错的地方,或者我错过了什么。
答案 0 :(得分:1)
JerseyJaxrsConfig
类是swagger-jersey2-jaxrs
库的一部分,因此在部署webapp时不可用,因为您将swagger-jersey-jaxrs
与Jersey 1.9结合使用(这很好) )。
简单地替换
<servlet-class>io.swagger.jaxrs.config.JerseyJaxrsConfig</servlet-class>
与
<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
使用正确的配置类,然后再次尝试访问http://localhost:8081/SampleRestProject/rest/swagger.json。
另外,在您考虑时,请考虑将Swagger的基本路径定义为相对路径,以便您能够在不同的端口上部署Web应用。
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>/SampleRestProject/rest</param-value>
</init-param>