【问题标题】:Using web.xml to configure JX-RS with Glassfish 4 is causing errors使用 web.xml 配置带有 Glassfish 4 的 JX-RS 会导致错误
【发布时间】:2014-06-12 17:37:23
【问题描述】:

我正在尝试使用 JX-RS 创建一个 Java EE 应用程序。我已经使用以下配置使其工作:

@ApplicationPath("rs")
public class MyApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<>();
        // register root resource
        classes.add(ProbeREST.class);
        return classes;
    }
}

但是,我更喜欢使用 web.xml 进行配置。我认为与简单的 xml 配置相比,上述内容非常难看,如下所示:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rs/*</url-pattern>
    </servlet-mapping>
</web-app>

很遗憾,当我尝试部署应用程序时,收到错误消息:

Exception while deploying the app [my_app] : There is no web component by the name of javax.ws.rs.core.Application here.

如何防止出现此错误?

【问题讨论】:

    标签: java glassfish jersey web.xml glassfish-4


    【解决方案1】:

    如 JAX-RS 2.0,2.3.2 Servlet 一章中所述,您确实错过了 web.xml 中的 servlet 条目:

    <?xml version="1.0" encoding="UTF-8"?>
       <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
            <servlet>
                <servlet-name>javax.ws.rs.core.Application</servlet-name>
                <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                <servlet-name>javax.ws.rs.core.Application</servlet-name>
                <url-pattern>/rs/*</url-pattern>
            </servlet-mapping>
        </web-app>
    

    【讨论】:

      【解决方案2】:

      web.xml 中的 servlet-mapping 是问题所在,只需将其删除即可。不需要,因为您正在部署到与 Servlet 3 兼容的容器,该容器支持无需 web.xml 的自动应用程序注册。

      如果你web.xml看起来像这样就足够了:

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

      另请参阅:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        • 1970-01-01
        • 2018-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-08
        相关资源
        最近更新 更多