I tried to write a simple RESTEasy example to see how it works. I found info here: http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/ http://www.mkyong.com/webservices/jax-rs/resteasy-hello-world-example/
It is really simple and I understood how it works from another Restful example that is similar and works just fine.
@Path("/person")
public class PersonResource {
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(PersonResource.class);
private final static String FIRST_NAME = "firstName";
private final static String LAST_NAME = "lastName";
private final static String EMAIL = "email";
private Person person = new Person(1, "Sample", "Person", "sample_person@jerseyrest.com");
@GET
@Produces(MediaType.TEXT_PLAIN)
public String respondAsReady() {
return "Entered PersonResource";
}
@GET
@Path("/get")
@Produces("application/json")
public Person getProductInJSON() {
return person;
}
@GET
@Path("sample")
@Produces(MediaType.APPLICATION_JSON)
public Response getSamplePerson() {
LOG.debug("getSamplePerson()");
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("Person First Name:", person.getFirstName());
jsonObject.put("Person Last Name:", person.getLastName());
} catch (JSONException e) {
LOG.debug("jsonObect.put failed");
}
String result = "jsonObject:" + jsonObject;
return Response.status(200).entity(result).build();
}
}
My web.xml :
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.restexample.PersonResource</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
My pom.xml:
<!-- RESTEasy-->
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.11.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.11.Final</version>
</dependency>
</dependencies>
When I try http://localhost:8080/rest/person/sample or any other path to acces the methods there is a blank screen. I DON'T have 404 NOT FOUND! Just blank screen. (I'm using TomCat). Can anyone help me?
I found out what was the problem. In my pom.xml I had this:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
This listener is creating an application context. Nothing wrong until now. The ResteasyBootstrap that is defined in web.xml provides as well a context:
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
Because both listeners are providing a different context, the URL doesn't return a 404 NOT FOUND, neither the good result.
The solution:
Remove the listener ContextLoaderListener in the xml and everything will work.
Try this:
@GET
@Path("sample")
@Produces(MediaType.APPLICATION_JSON)
public Response getSamplePerson() {
LOG.debug("getSamplePerson()");
return Response.status(Status.OK).entity(person).build();
}
Try add this in your web.xml :
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
https://docs.jboss.org/resteasy/docs/1.0.1.GA/userguide/html/Installation_Configuration.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.