Mulgard
Mulgard

Reputation: 10609

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:

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.

Upvotes: 1

Views: 16131

Answers (2)

Marcel St&#246;r
Marcel St&#246;r

Reputation: 23565

Why don't you just follow the Getting Started guide? It tells you exactly how to go about this.

You'd see that you need, amongst a few others, these JARs in WEB-INF/lib:

javax.annotation-api-1.2.jar
javax.inject-2.1.88.jar
javax.ws.rs-api-2.0.jar
jersey-client-2.0.jar
jersey-common-2.0.jar
jersey-container-servlet-core-2.0.jar
jersey-server-2.0.jar

You'd also see that the web.xml needs to look similar to this:

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

Upvotes: 1

Mulgard
Mulgard

Reputation: 10609

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

Upvotes: 1

Related Questions