当前位置: 首页 > 知识库问答 >
问题:

JAX RS泽西JSON-->HTTP 500内部服务器错误

督飞羽
2023-03-14

我的网络服务有一些问题,我试图从网络服务接收我的电影列表,但总有这个严重的错误。

这是我的模型:

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

}

还有一把刀和一个门面,但它们工作得很好!

以下是我的服务:

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”和“count”运行良好,我将在浏览器中显示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)

共有2个答案

竺辉
2023-03-14

尝试将RequestScoped添加到REST服务。

邓丰
2023-03-14

我有同样的问题,但与GAE(谷歌应用程序引擎)。这似乎与Jersey MessageBodyWriter无法序列化JSON有关。

我通过添加genson解决了这个问题。jar到我的类路径。Genson是一个JSON序列化程序/反序列化程序库,可以在Genson项目页面上下载。

您还可以使用MOXy作为您的JSON提供程序(因为它是支持JSON与泽西绑定的首选方式)。

不要忘记将jar复制到您的WEB-INF/lib文件夹中。

 类似资料:
  • 我正在学习JAX-RS,在发出生成JSON的GET请求时,出现HTTP Status 500内部服务器错误。XML工作正常。 我搜索了其他问题,但找不到解决方案。这可能是因为我目前缺乏知识。然而,据我所知,有人建议我的项目在构建路径中缺少一个JAR。 我没有使用Maven。我刚刚在Eclipse中创建了一个动态Web应用程序,并将JAX-RS/jerjar添加到我的WEB-INF/lib目录中,并

  • 当我尝试在任何一点使用定位器时,我仍然无法创建我使用locator.create(mything.class)方法在AbstractBinder中注册的东西的实例。 我确信它们是正确绑定的,因为它们通过@inject字段注释被正确地注入到我的资源类中。 不同的是,Jersey/HK2框架正在实例化我的资源类(正如预期的那样,因为它们在我的包扫描路径中),但我似乎无法通过代码来利用ServiceLo

  • 我正在尝试使用内置的HTTP服务器从命令行运行一个简单的泽西应用程序。 根据各种教程,我将我的应用程序设置为这样: src/main/java/net/wjlafrance/jerseyfun/App.java: src/main/WEB app/we b-INF/WEB . XML: 当我运行时,我看到这样的输出: 显然,我的服务器配置错误。有人能给我指出正确的方向吗?

  • 我是JavaWeb服务的新手,我正在努力解决一个基本问题。 在找到了一堆过时的示例后,我设法找到了一些使用XML的方法,但是当我要求它返回JSON时,相同的代码就不起作用了。 起初我认为它缺少JSON格式化程序,但JAXB应该负责从POJO到JSON的转换,所以我认为这不是问题所在。 Tomcat中引发的错误是: 网状物。XML Todo.java TodoResource。Java语言 你知道为

  • 问题内容: 我使用Jersey 1.11做了一个小型Rest Web服务。当我调用返回Json的url时,非英文字符的字符编码存在问题。Xml的相应网址(“ test.xml”在开始的xml-tag中使其成为utf-8。 如何使网址“ test.json”返回utf-8编码的响应? 这是该服务的代码: 这是服务使用的pojo: 问题答案: Jersey在默认情况下应始终生成utf-8,听起来像是您

  • 我是新的角度6。我用spring boot开发了一个post服务,当我通过postman测试它时,它工作得很好,但当我用web浏览器测试它时,它给了我这个错误: HttpErrorResponse ;{headers:HttpHeaders,status:500,statusText:“ok”,URL:“http://localhost:8080/api/test/ordermiss”,ok:fa