2013-07-06 74 views
0

我的web服務出現了一些問題-_- 我試圖從web服務接收我的電影列表,但總是出現這個令人討厭的錯誤。JAX RS Jersey + JSON - > HTTP 500內部服務器錯誤

這裏是我的模型:

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 

} 

還有一個DAO和一個門面,但他們工作的罰款!

這裏是我的服務:

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); 
    } 

} 

的梅索德「onemovie」和「伯爵」的作品很好,我會顯示在瀏覽器中的XML,但是當我稱之爲「XMLForApp」,那麼服務器拋我異常。

我的客戶是在這裏:

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; 
    } 
} 

的例外是:

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) 

回答

0

嘗試增加@RequestScoped到REST服務。

2

我有同樣的問題,但與GAE(谷歌應用程序引擎)。這似乎與Jersey MessageBodyWriter無法序列化JSON相關。

我通過將genson.jar添加到我的類路徑中解決了這個問題。 Genson是一個JSON串行器/解串器庫,您可以在genson project page上下載。

您還可以使用MOXy作爲您的JSON提供程序(因爲這是支持與Jersey綁定JSON的首選方式)。

不要忘記將jar複製到您的WEB-INF/lib文件夾中。

相關問題