0

I'm having some problems with my webservice -_- I try to receive my movie list form the webservice, but there always this nasty error.

here is my model:

package de.hawhof.dm.kino.model;

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

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement(name="movie")
public class Movie implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int movieid;
    String name;
    String releaseDate;
    @Lob
    String critic;
    String runtime;
    @Lob
    String synopsis;
    String posterPath;
    String thumbnail;

    // user movie comment rating
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "movie")
    List<User_Movie_CR> userMovieCR;

    // cinema movie
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "movie")
    List<Cinema_Movie> cinemaMovie;

    // user film cinema table
    @OneToMany(mappedBy = "movie")
    Set<Cinema_Movie_User> cinema_movie_user;

    public Movie() {

    }

    public Movie(String name, String releaseDate, String critic,
            String runtime, String synopsis, String posterPath, String thumbnail) {
        this.name = name;
        this.releaseDate = releaseDate;
        this.critic = critic;
        this.runtime = runtime;
        this.synopsis = synopsis;
        this.posterPath = posterPath;
        this.thumbnail = thumbnail;
    }

//   toString
    public String toString() {
        return "Movie [name=" + name + ", releaseDate=" + releaseDate
                + ", critic=" + critic + ", runtime=" + runtime + ", synopsis="
                + synopsis + ", posterPath=" + posterPath + "]";
    }

    // setter and getter

}

There is also a dao and a facade, but they are working fine!

here is my service:

package de.hawhof.dm.kino.ws;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import de.hawhof.dm.kino.facade.MovieFacade;
import de.hawhof.dm.kino.model.*;

@Path("/movies")
public class MoviesResource {

    @Context
    UriInfo uriInfo;
    @Context
    Request request;
    MovieFacade movieFacade = new MovieFacade();

    @GET
    @Path("xml")
    @Produces(MediaType.TEXT_XML)
    public List<Movie> getMoviesBrowser() {
        List<Movie> movies = new ArrayList<Movie>();
        movies.addAll(movieFacade.listAll());


        return movies;
    }

    @GET
    @Path("XMLForApp")
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public List<Movie> getMovies() {
        List<Movie> movies = new ArrayList<Movie>();
        movies.addAll(movieFacade.listAll());
        return movies;
    }

    @GET
    @Path("onemovie")
    public Movie getMovie() {
        return new Movie("TEST!!!", "", "", "", "", "", "");
    }

    @GET
    @Path("count")
    @Produces(MediaType.TEXT_PLAIN)
    public String getCount() {
        int count = movieFacade.listAll().size();
        return String.valueOf(count);
    }

    @Path("{movie}")
    public MovieResource getMovie(@PathParam("movie") int movieid) {
        return new MovieResource(uriInfo, request, movieid);
    }

}

the methode "onemovie" and "count" works fine and i will get the xml displayed in my browser,but when i call "XMLForApp" then the server throw me exception.

my client is here:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;


public class MovieWSClient {

    Client client;
    String movieList;

    public MovieWSClient() {
        client = ClientBuilder.newClient().register(getClass());
        movieList = client.target("http://localhost:8080/kino").path("/rest/movies/XMLForApp").request(MediaType.APPLICATION_JSON).get(String.class);
    }

    public String getMovieList() {
        return movieList;
    }
}

the exception is:

SEVERE: Servlet.service() for servlet [Hello Servlet] in context with path [/kino] threw exception [An exception occurred processing JSP page /pages/public/hello.jsp at line 20

Stacktrace:] with root cause
javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:904)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:749)
    at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:88)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:421)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:375)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:275)
    at de.hawhof.dm.kino.ws.MovieWSClient.<init>(MovieWSClient.java:16)
    at org.apache.jsp.pages.public_.hello_jsp._jspService(hello_jsp.java:77)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at de.hawhof.dm.kino.servlet.HelloServlet.doGet(HelloServlet.java:21)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
FDinoff
  • 30,689
  • 5
  • 75
  • 96
user2346226
  • 31
  • 1
  • 5

2 Answers2

2

I had the same issue but with GAE( google app engine). It's seems related to Jersey MessageBodyWriter not able to serialize the JSON.

I solved it by adding genson.jar to my classpath. Genson is a JSON serializer/deserializer library that you can download on genson project page.

You can also use MOXy as your JSON provider (since it's the preferred way of supporting JSON binding with Jersey).

Don't forget to copy the jar into your WEB-INF/lib folder.

CtrlX
  • 6,946
  • 1
  • 17
  • 21
0

Try adding @RequestScoped to the REST service.

HankCa
  • 9,129
  • 8
  • 62
  • 83