使用不带web.xml的ResourceConfig的Jersey应用程序的URL

时间:2014-04-08 21:32:20

标签: java jersey tomcat7

我使用带有Jersey 2.7的ResourceConfig并在Tomcat 7上进行部署,从web.xml迁移到完全Java配置。之后,我无法通过使用与web.xml方法相同的URL来访问服务。我不明白ResourceConfig如何影响路径。

我之前的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    
version="3.0">
<servlet>
    <servlet-name>my.app</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.mypackage.resource,com.mypackage.providers</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.scanning.recursive</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>        
    </init-param>
    <init-param>
        <param-name>org.glassfish.jersey.server.ServerProperties.BV_SEND_ERROR_IN_RESPONSE</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>my.app</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

我扩展ResourceConfig的配置类是:

MyRESTAPIApp.java

@ApplicationPath("")
public class MyRESTAPIApp extends ResourceConfig{
    public MyRESTAPIApp () {
        packages("com.mypackage.resource", "com.mypackage.providers");
        register(org.glassfish.jersey.filter.LoggingFilter.class);
        property("jersey.config.beanValidation.enableOutputValidationErrorEntity.server", "true");
    }
}

我的一个资源是:

FlagResource.java

@Path("my-resource")
public class FlagResource {
private MyService myService = new MyService();

@GET
@Produces(MediaType.APPLICATION_JSON)
public FlagResource getFlagResource(@NotNull @QueryParam("level") Long level) {
    FlagResource flagResource = myService.getFlagResource(level);
    return flagResource;
}

}

我正在生成的战争叫做:my.app.war。

Tomcat像往常一样从war文件的名称中获取Web上下文根路径,但在使用基于Java代码的配置时,我不知道这是否会发生变化。

GET http://localhost:8080/my.app/my-resource?level=1

返回404

1 个答案:

答案 0 :(得分:8)

实际上我通过添加“/”作为@ApplicationPath注释的值来解决这个问题,我认为没有必要,因为API文档为@ApplicationPath值param说了以下内容:

Defines the base URI for all resource URIs. A trailing '/' character will be automatically appended if one is not present.

我假设留下一个空字符串相当于使用@ApplicationPath(“/”),但它不是。

这就是配置类现在的样子:

@ApplicationPath("/")
public class MyRESTAPIApp extends ResourceConfig{
    public MyRESTAPIApp () {
        packages("com.mypackage.resource", "com.mypackage.providers");
        register(org.glassfish.jersey.filter.LoggingFilter.class);
        property("jersey.config.beanValidation.enableOutputValidationErrorEntity.server", "true");
    }
}