Jersey + Tomcat8

时间:2017-10-29 07:19:46

标签: java maven jersey swagger tomcat8

的pom.xml

<dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-jersey2-jaxrs</artifactId>
                <version>1.5.7</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.glassfish.jersey.containers</groupId>
                        <artifactId>jersey-container-servlet-core</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">


    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>io.swagger.jaxrs.listing,
            rs
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>



     <servlet>
        <servlet-name>Jersey2Config</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.title</param-name>
            <param-value>Swagger APIzz</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8080/SwaggerExample/webapi</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.pretty.print</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.scan</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet> 
</web-app>

SwaggerExample是上下文根

PeopleRestService.java存在于rs包中

@Path( "/people" ) 
@Api( value = "/", description = "Manage people" )
public class PeopleRestService {

     private static PeopleService peopleService;

    static{
        peopleService = new PeopleService();
    }

    @Produces( { MediaType.APPLICATION_JSON } )
    @GET
    @ApiOperation( value = "List all people", notes = "List all people using paging", response = Person.class, responseContainer = "List")
    public Collection< Person > getPeople(  @ApiParam( value = "Page to fetch", required = true ) @QueryParam( "page") @DefaultValue( "1" ) final int page ) {
        System.out.println("inside service........");
        return peopleService.getPeople( page, 100 );
    }
}

上面的代码可以通过webservice获取数据,但在访问swagger.json时,它会给出404

  

http://localhost:8080/SwaggerExample/webapi/people:工作正常

     

http://localhost:8080/SwaggerExample/webapi/swagger.json:正在给予404

2 个答案:

答案 0 :(得分:0)

如果你可以在swagger UI上看到你的API,那就意味着swagger UI知道你的swagger.json在哪里,在我的情况下,我去了index.html并搜索了swagger.json

我找到了这段代码

var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
    url = decodeURIComponent(url[1]);
} else {
    url = "/my-app-name/app/swagger.json"; << this
}

修改1:

newer versions中,您会找到类似

的内容
  const ui = SwaggerUIBundle({
  url: "http://petstore.swagger.io/v2/swagger.json", << this
  dom_id: '#swagger-ui',
  deepLinking: true,
  presets: [
    SwaggerUIBundle.presets.apis,
    SwaggerUIStandalonePreset
  ],
  plugins: [
    SwaggerUIBundle.plugins.DownloadUrl
  ],
  layout: "StandaloneLayout"
})

我猜您还必须相应地更换init-param,以便远程访问它。

<param-value>http://localhost:8080/SwaggerExample/webapi</param-value>

到相对路径

<param-value>/SwaggerExample/webapi</param-value>

答案 1 :(得分:0)

在web.xml中替换

<init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>io.swagger.jaxrs.listing,
            rs
            </param-value>
        </init-param>

<init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>
               services.ConfigApplication
            </param-value>
        </init-param> 

并明确注册类

class ConfigApplication extends ResourceConfig{

    public ConfigApplication(){

        register(ApiListingResource.class);
        register(SwaggerSerializers.class);
        register(PeopleRestService.class);
    }

}