** 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;
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).
Alvin Thompson Nov 03 '14 at 19:38 2014-11-03 19:38
source share