4

I am trying to create a Java EE application utilizing JX-RS. I have got it working using the following configuration:

@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;
    }
}

However, I would much prefer to use web.xml for the configuration. I think the above is very ugly in comparison to a simple xml configuration, like so:

<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>

Unfortunately, when I try to deploy the application, I receive the error:

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

How can I prevent this error?

Kevin
  • 4,070
  • 4
  • 45
  • 67

2 Answers2

5

As described in JAX-RS 2.0, chapter 2.3.2 Servlet, you do miss the servlet entry in your web.xml:

<?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>
Marcel
  • 266
  • 2
  • 5
1

The servlet-mapping in your web.xml is the problem, just remove it. It is not needed because you are deploying to a Servlet 3 compatible container, which supports automatic application registration without web.xml.

It should be sufficient if you web.xml looks like this:

<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>

See also:

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66