Reputation: 2403
I am trying to get working a simple JAX RS example, but I am failing to do so.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>PLAYGROUND</display-name>
<servlet-mapping>
<servlet-name>playground.Rest</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
Rest.java
package playground;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
public class Rest extends Application {
@GET
@Path("hello")
public String helloworld() {
return "Hello World!";
}
}
Accessing http://localhost/{warcontext}/api/hello
with the browser (GET) gives me 404 error status
It is probably something very silly but I can't figure out.
Using: JBoss EAP 6.1.0 (Java EE 6)
Upvotes: 5
Views: 22911
Reputation: 5448
I also had issues getting this to work, so I'm posting a more complete how-to for posterity:
If you're using a standard Tomcat install (or some other servlet container), AFAIK you can't avoid explicitly telling it what servlets to start in the web.xml
file*. Since you have to use web.xml
anyway, the simplest way to get restful web services working is to forget extending javax.ws.rs.core.Application
entirely and just specify the context path there. You can still use standard jax-rs annotations to declare the actual web services.
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
>
<servlet>
<servlet-name>rest-test</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.domain.mypackage</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name> rest-test</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Two noteworthy points:
You will need to bundle a REST implementation in your WAR file, since servlet containers don't usually contain one. Since Jersey is the reference implementation for JAX-RS, that's the one I'm using in the servlet-class
element above. You can replace this with Apache CXF implementation if you want.
The init-param
element tells Jersey which of your packages to search for Java files with web service annotations. Edit this to point to your web services. Note that if you opt to use apache CXF instead of Jersey, the stuff needed in any init-param
elements will be different. Someone who knows CXF please post what they would be.
If you're using Maven, just add a dependency to jersey-servlet
in the dependencies
section of your pom.xml
file:
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18.2</version>
</dependency>
...
</dependencies>
After this, declaring your web services is straight forward using the 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's 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);
}
}
This should be all you need. If your Tomcat install is running locally on port 8080 and you deploy your WAR file to the context myContext
, going to
http://localhost:8080/myContext/rest/calc/1.0/addTwoNumbers;firstNumber=2;secondNumber=3
...should produce the expected result (5).
Cheers!
* Someone please correct me if you know of a way to a add the Jersey servlet to the context in Tomcat without using web.xml
--maybe by using a context or life cycle listener?
Upvotes: 0
Reputation: 1106
web.xml
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>javax.ws.rs.core.Application</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Rest.java
public class Rest {
@GET
@Path("hello")
public String helloworld() {
return "Hello World!";
}
}
If you need, you can add
YourApplication.java
package playground;
import javax.ws.rs.core.Application;
...
public class YourApplication extends Application {
@Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> yourResources = new HashSet<Class<?>>();
yourResources.add(InvoiceResource.class);
return yourResources;
}
}
Then
web.xml
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>playground.YourApplication</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
If you add @ApplicationPath("/api")
for YouApplication
, then web.xml shouldn't have servlet-mapping
.
Upvotes: 7
Reputation: 1256
You need to declare servlet-class in web.xml (servlet name is not the same):
<servlet>
<servlet-name>Playground REST services</servlet-name>
<servlet-class>your.package.playground.Rest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Playground REST services</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Use the full name of your class (including package path name) in servlet-class.
Upvotes: 0
Reputation: 1600
You need to extend javax.ws.rs.core.Application (it can remain empty) and annotate it with @ApplicationPath("/ide"), then create a JAX-RS resource, ie, a class with an @Path("/hello") annotation. In this class, you'll just need to have your JAX-RS Resource Method annotated with @GET.
@ApplicationPath("/ide")
public class Rest extends Application { }
@Path("/hello")
public class HelloResource {
@GET
@Path("hello")
public String helloworld() {
return "Hello World!";
}
}
You can also take a look at this example: https://github.com/resteasy/Resteasy/tree/master/jaxrs/examples/oreilly-workbook/ex03_1
Upvotes: 6