Issue
I'm new to the REST application by using the JAX-RS standard and I want to learn how to work with this standard.
So I downloaded and setup Apache Tomcat 9.0 and I added the <Context>
like this:
<Context docBase="...\RestEasyApp\target\RestEasyApp" path="/example" reloadable="true"/>
Then with Maven
I created my project that have this structure:
Then into the pom.xml
i added RestEasy by this dependencies:
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.5.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-servlet-initializer -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.5.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson2-provider -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.5.1.Final</version>
</dependency>
Then into the web.xml
I created the Servlet and it looks like this:
<web-app>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Then finally i created the Hello.java class that will handle the request and it looks like this:
package com.pisi.resteasyapp;
import javax.ws.rs.*;
@Path("/api")
class Hello {
@GET
@Path("/hello")
public String hello() {
return "HELLO";
}
}
But when I run this App with this uri http://localhost:8080/example/rest/api/hello
, it gives me this:
I have no clue why it wont work.
Solution
You must add an Application
class to your project:
package com.pisi.resteasyapp;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("")
public class HelloApplication extends Application {
@Override
public Set<Object> getSingletons() {
Set<Object> set = new HashSet<>();
set.add(new Hello());
return set;
}
}
You have also to change the mapping of the Hello
class from @Path("/api")
to @Path("/rest/api")
and make the class public
to allow RESTEasy to reflect on the hello()
method:
@Path("/rest/api")
public class Hello {
In general, a better solution would be:
- remove the
<servlet-mapping>
fromweb.xml
- change
@ApplicationPath("")
to@ApplicationPath("/rest")
in theHelloApplication
class - use
@Path("/api")
instead of@Path("/rest/api")
in theHello
class
Answered By - Robert Hume