How to configure JAX-RS application using only annotations (without web.xml)?

Is it possible to configure the JAX-RS application only with annotations? (using Servlet 3.0 and JAX-RS Jersey 1.1.0)

I tried and out of luck. It is recommended to use some web.xml .




Configuration A (working, but has web.xml configuration)

web.xml

  ... <servlet> <servlet-name>org.foo.rest.MyApplication</servlet-name> </servlet> <servlet-mapping> <servlet-name>org.foo.rest.MyApplication</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> ... 

Java

 @ApplicationPath("/") public class MyApplication extends Application { ... } 



Configuration B (not working, exception thrown)

 @ApplicationPath("/") @WebServlet("/*") // <-- public class MyApplication extends Application { ... } 

The latter seems to insist that the application will be a subclass of Servlet (the exception leaves no clue)

 java.lang.ClassCastException: org.foo.rest.MyApplication cannot be cast to javax.servlet.Servlet 



Questions

  • Why did the definition of web.xml work, but there was no annotation? What's the difference?

  • Is there a way for it to work, for example. Is there a JAX-RS application without web.xml?

+65
java-ee jax-rs
Feb 21 '12 at 6:19 06:19
source share
6 answers

It seems that all I needed to do was this (Servlet 3.0 and above)

 @ApplicationPath("/*") public class MyApplication extends Application { ... } 

And apparently no web.xml configuration was (tested on Tomcat 7)

+45
Feb 21 '12 at 18:21
source share

** PLEASE READ IF YOU ARE USING TOMATCO OR HARD! **

The accepted answer works , but only if the webapp is deployed to an application server such as Glassfish or Wildfly, and possibly servlet containers with EE extensions such as TomEE. It does not work in standard servlet containers like Tomcat, and I'm sure most people looking for a solution here want to use.

If you are using a standard Tomcat installation (or some other servlet container), you need to enable the REST implementation since Tomcat does not come with it. If you are using Maven, add this to the dependencies section:

 <dependencies> <dependency> <groupId>org.glassfish.jersey.bundles</groupId> <artifactId>jaxrs-ri</artifactId> <version>2.13</version> </dependency> ... </dependencies> 

Then just add the application configuration class to your project. If you do not have any special configuration needs other than setting the context path for other services, the class may be empty. After adding this class, you do not need to configure anything in web.xml (or have it at all):

 package com.domain.mypackage; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("rest") // set the path to REST web services public class ApplicationConfig extends Application {} 

After that, your web services are advertised using standard JAX-RS annotations in your Java classes:

 package com.domain.mypackage; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import javax.ws.rs.GET; import javax.ws.rs.MatrixParam; import javax.ws.rs.Path; // It good practice to include a version number in the path so you can have // multiple versions deployed at once. That way consumers don't need to upgrade // right away if things are working for them. @Path("calc/1.0") public class CalculatorV1_0 { @GET @Consumes("text/plain") @Produces("text/plain") @Path("addTwoNumbers") public String add(@MatrixParam("firstNumber") int n1, @MatrixParam("secondNumber") int n2) { return String.valueOf(n1 + n2); } } 

That should be all you need. If your Tomcat installation is performed locally on port 8080 and you deploy your WAR file to the myContext context, go to ...

 http://localhost:8080/myContext/rest/calc/1.0/addTwoNumbers;firstNumber=2;secondNumber=3 

... should produce the expected result (5).

+133
Nov 03 '14 at 19:38
source share

Chapter 2 of the JAX-RS Specification : Java ™ API for RESTful Web Services describes the process of publishing a JAX-RS application in the Servlet environment (section 2.3.2 of the Servlet in the specification).

Please note that it is recommended to use the Servlet 3 environment (section 2.3.2 Servlet, page 6):

It is RECOMMENDED that implementations support Servlet 3 to ensure portability between containers and use the container class of the scan tool.

In short, if you want to use the no-web.xml approach, perhaps with a special javax.ws.rs.core.Application implementation that registers RESTful service resources using the javax.ws.rs.ApplicationPath annotation.

 @ApplicationPath("/rest") 

Although you specifically asked about Jersey, you might also like to read the article Deploying RESTful Services with JAX-RS and WebSphere 8.5 Liberty Profile , in which I described the Non-Publishing process web.xml for the WebSphere Liberty Profile (with Apache Wink as a JAX implementation -RS).

+14
Apr 01 '13 at 21:18
source share

You need to configure the correct dependencies in pom.xml

 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> </dependency> 

Read more here: Launch example for jax-rs

+5
Aug 19 '15 at 7:57
source share

As Eran-Medan noted, JBoss EAP 7.1 (note that without a web application, therefore there is no servlet, I did this in the EJB 3.2 project) I had to add a value attribute, since I received an exception that the value attribute was necessary .

It worked for me

  @ApplicationPath(value="/*") public class MyApplication extends Application { private Set singletons = new HashSet(); public MyApplication() { singletons.add(new MyService()); } ... } 

Stack trace

  Caused by: java.lang.annotation.IncompleteAnnotationException: javax.ws.rs.ApplicationPath missing element value at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:80) at com.sun.proxy.$Proxy141.value(Unknown Source) ... 21 more 
0
May 25 '17 at 20:01
source share

The previously mentioned dependencies did not work for me. From the Jersey user guide:

Jersey provides two Servlet modules. The first module is a core Servlet module that provides basic support for servlet integration and is required in any Servlet 2.5 or higher container:

 <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> </dependency> 

To support additional deployment modes of Servlet 3.x and the asynchronous JAX-RS resource programming model, an additional Jersey module is required:

 <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> </dependency> 

The jerc-container-servlet module depends on the kernel-container-servlet-kernel module, so when it is used, there is no need to explicitly declare the jerc-container-servlet kernel dependency.

https://jersey.imtqy.com/documentation/latest/deployment.html#deployment.servlet.3

0
Mar 05 '18 at 18:53
source share



All Articles