Mulgard Mulgard - 6 months ago 22
Java Question

Restful JAX-RS Server Error

I have problems using Restful Webservices using the Jersey implementation.
In the documentation it is written that i should download this :
Jersey JAX-RS 2.0 RI bundle, unzip it and add the jars to my WEB-INF/lib
folder. These jars are:


  • jersey-client

  • jersey-common

  • jersey-container-servlet

  • jersey-container-servlet-core

  • jersey-server



If i do so i always get the following exception:

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer


After some researching i found out that i should add jersey-servlet-1.12.jar instead
of adding the jars from the download source posted above. So I did it. I added
this to my web.xml

<servlet>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>


And i implemented a small test-class:

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import de.hof.university.spj.model.Movie;

@Path("/test")
public class Test {

@GET
@Produces("application/json; charset=UTF-8")
public List<Movie> getMovieList() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("SPJUnit");
EntityManager em = factory.createEntityManager();

TypedQuery<Movie> query = em.createQuery("Select m FROM Movie m WHERE m.movieID = 4204", Movie.class);

return query.getResultList();
}
}


When i now call
http://localhost:8080/MyApp/jaxrs/test
i get this error:

HTTP Status 500 - Internal Server Error. The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.


I dont know how to do use the jersey implementation correctly.

Answer

I solved the Problem.

I deleted all jersey jars and i deleted the webservices part from the web.xml.

I changed my test class to this:

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import de.hof.university.spj.model.Movie;

@RequestScoped
@ApplicationPath("/webservices")
@Path("/movies")
public class Test extends Application implements Serializable {
    private static final long serialVersionUID = 1L;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Movie> getMovieList() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("SPJUnit");
        EntityManager em = factory.createEntityManager();

        TypedQuery<Movie> query = em.createQuery("Select m FROM Movie m WHERE m.movieID = 4204", Movie.class);

        return query.getResultList();
    }
}

And i added @XmlRootElement to my Movie Entity class.

Now it works fine