5

I am trying to deploy a really simple jaxrs application with without a web.xml config and cannot get it working. My URL I'd expect to access is serverandport/{appname}/rest/welcomes/hello and I think I must be missing something dead obvious.

Application

@ApplicationPath("/rest")
public class EngineApp extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(RestTestImpl.class);
        return s;
    }

}

Resource

@Path("/welcomes")
public class RestTestImpl {
    @GET
    @Path("hello")
    public String sayPlainHello() {
        return "Hi from Rest";
    }
}

POM snippet

<groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>

Edit: Further to the response below, I tried with an empty web.xml and also with the following web.xml. Bother also return 404, however the xml below states "Servlet javax.ws.rs.core.Application is not available":

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
    <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>/*</url-pattern>
    </servlet-mapping>

</web-app>
edwardmlyte
  • 15,937
  • 23
  • 58
  • 83

3 Answers3

3

You need to deploy your application into a Servlet 3.0 compliant container to take advantage of this functionality. Try GlassFish 3.x or Tomcat 7.

Michal Gajdos
  • 10,339
  • 2
  • 43
  • 42
  • I just tried this after reading about servlet 3.0 support (see examples 2.8 and 2.9 in the [jersey documentation](http://jersey.java.net/nonav/documentation/latest/jax-rs.html)). GF3 works fine, same application on Tomcat 7 throws an AbstractMethodError on javax.ws.rs.core.UriBuilder.uri. Anyway, as GF3 works I'll mark your answer as correct. Thanks. – edwardmlyte Nov 14 '12 at 15:39
  • 1
    This does **not** work on servlet containers like Tomcat. Please see my answer [here](http://stackoverflow.com/questions/9373081/how-to-set-up-jax-rs-application-using-annotations-only-no-web-xml). – Alvin Thompson Nov 03 '14 at 20:00
0

I have found a quite similar question and the problem found there could help you as well. I would definitely try it with an empty web.xml with 3.0 schema version to see if it helps.

JBoss AS 7 Restful Webservices not auto deploying

Community
  • 1
  • 1
jabal
  • 11,987
  • 12
  • 51
  • 99
  • I updated my Q to include the web.xml I tried. An empty one and the one above also didn't help matters. Though I did get a more detailed error message. – edwardmlyte Nov 14 '12 at 10:35
-3

For those wishing to use the Servlet 2.0 specification, create a web.xml and add in the jersey servlet maven dependency:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.mycompany.rest.engine.EngineApp</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

Add dependency to pom

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.13</version>
</dependency>
edwardmlyte
  • 15,937
  • 23
  • 58
  • 83