【问题标题】:URL of a Jersey Application using ResourceConfig without web.xml使用没有 web.xml 的 ResourceConfig 的 Jersey 应用程序的 URL
【发布时间】:2014-04-08 21:32:19
【问题描述】:

我使用带有 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

【问题讨论】:

    标签: java jersey tomcat7


    【解决方案1】:

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

    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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 2016-04-24
      • 1970-01-01
      相关资源
      最近更新 更多