Jackson JSON生成HTTP状态500,XML工作 [英] Jackson JSON generates HTTP status 500, XML works

查看:191
本文介绍了Jackson JSON生成HTTP状态500,XML工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Jackson将Java对象序列化为JSON和XML,作为REST Web服务的响应。我有以下带注释的Java对象:

I am currently using Jackson to serialize Java objects to JSON and XML as the response for a REST webservice. I have the following annotated Java object:

@XmlRootElement(name = "Product")
@XmlAccessorType(XmlAccessType.FIELD)
public class ProductDetailsView {

   @XmlElement
   private int id;
   @XmlElement
   private long EAN;
   @XmlElement
   private String manufacturer;
   @XmlElement
   private String modelname;
   @XmlElementWrapper(name = "onlineCompetitors")
   @XmlElement(name = "competitor")
   private List<OnlineCompetitorView> onlineCompetitors;

现在,前几个字段是原始类型,它们在JSON和XML中完美地工作(实际上,有更多的原始领域)。但是,从我添加OnlineCompetitorView列表的那一刻起,JSON序列化就不再起作用,并导致应用程序生成HTTP状态500(内部服务器错误)。但是,当我使用application / xml作为接受的内容类型时,它可以完美地工作。

Now, the first few fields are primitive types and those work perfectly in both JSON and XML (in fact, there are a lot more primitive fields). However, from the moment I added the list of OnlineCompetitorView's the JSON serialization no longer works, and causes the application to generate a HTTP status of 500 ('Internal Server Error'). But, when I use application/xml as the accepted content type it works flawlessly.

正确的XML响应:

<Product>
   <id>1</id>
   <EAN>5901234123457</EAN>
   <manufacturer>Samsung</manufacturer>
   <onlineCompetitors>
      <competitor>
         <id>1</id>
         <shopname>Shop1</shopname>
         <rating>4</rating>
         <productPrice>488.95</productPrice>
         <stock>7</stock>
      </competitor>
      <competitor>
         <id>2</id>
         <shopname>Shop2</shopname>
         <rating>5</rating>
         <productPrice>498.95</productPrice>
         <stock>12</stock>
      </competitor>
   </onlineCompetitors>
</product>

因此XML工作正常,但是当我从服务请求application / json时(GlassFish 4.0)创建内部服务器错误。这是OnlineCompetitorView的注释方式:

So the XML works fine, but when I request application/json from the service it (GlassFish 4.0) creates an internal server error. This is how the OnlineCompetitorView is annotated:

@XmlAccessorType(XmlAccessType.FIELD)
public class OnlineCompetitorView {

    @XmlElement
    private final int id;
    @XmlElement
    private final String shopname;
    @XmlElement
    private final int rating;
    @XmlElement
    private final double productPrice;
    @XmlElement
    private final int stock;

我也试过在OnlineCompetitorView中添加@ XmlRootElement-annotation,但这并没有改变任何东西。由于我没有从GlassFish收到任何错误,我真的不知道如何解决问题。简单的web服务如下所示:

I've tried adding the @XmlRootElement-annotation the OnlineCompetitorView as well, but this doesn't change anything. As I'm not getting any error from GlassFish I really don't know how to fix the problem. The simple webservice looks like this:

@GET
@Path("/get/product/{ean}")
@Produces({"application/xml", "application/json"})
public ProductDetailsView getProduct(@PathParam("ean") Long EAN) {
    ProductDetailsView pdv = service.getProductDetailsView(EAN);
    return pdv;
}

那么XML如何正常工作,而JSON创建内部服务器错误?
有人可以帮我解决这个问题吗?非常感谢任何帮助!

So how is it possible that XML works fine, while JSON creates an internal server error? Could someone help me out with this problem? Any help is greatly appreciated!

编辑!

由于我仍然不知道为什么JSON不起作用,而XML确实在继续开发我的应用程序。在这样做时,我偶然发现了一个新问题,可能与第一个问题有关,这就是为什么我更新我的问题。

As I still don't know why JSON isn't working, while XML does I went on developping my application. In doing so, I stumbled upon a new problem, probably relating to the first one so that's why I update my question.

我创建了一个小而简单的测试返回一个非常简单的带注释的POJO,它不能与XML或JSON一起使用(虽然更复杂的'Product'-POJO正在工作,尽管只有XML)。

I created a small and simple test which returns back a very simple annotated POJO and it doesn't work with either XML or JSON (while the much more complex 'Product'-POJO is working, albeit only with XML).

现在,错误日志仍然没有显示错误,但是glassfish返回了一些东西:

Now, the error log still shows no errors but glassfish returns me something:

FINE: Trying to locate com/eid/instoreapp/JSONView/jaxb.properties
FINE: not found
FINE: Checking system property javax.xml.bind.context.factory 
FINE: not found
FINE: Checking system property javax.xml.bind.JAXBContext
FINE: not found
FINE: OSGi environment detected



<我一直在研究这个问题,显然JAXB需要某种属性文件。现在,我已经构建了更多这样的REST应用程序,我从来没有必要添加属性文件,它总是有效。

I've been doing some research about this and apparently JAXB needs some sort of properties file. Now, i've build a lot more REST applications like this and I never had to add a properties file or whatsoever, and it always worked.

互联网上的一些人建议这是类加载器的问题(如这里)但是,再一次,我从来没有设置过一些类加载器,因为我使用Java EE,这种XML / JSON对象的编组和解组应该自动运行。此外,超链接中提出的解决方案(添加类加载器XML属性)对我不起作用。

Some people on the internet suggest that it is a problem with classloader (like here) but, once again, I never had to set some classloader and because I use Java EE this marshalling and unmarshalling of XML/JSON objects should work automatically. Also, the solution proposed in the hyperlink (adding the class-loader XML attribute) doesn't work for me.

我不明白为什么系统如此不一致。一些RESTful方法适用于XML和JSON。其中一些只能使用XML,其中一些根本不起作用(在同一个应用程序中!)。

I don't understand why the system is so inconsistent. Some of the RESTful methods work in both XML and JSON. Some of them work only with XML and some of them don't work at all (within the same application!).

我真的希望有人能帮我找到解决方案对于这个问题!

I really hope anyone can help me find a solution to this problem!

推荐答案

对于那些遇到与我一样的问题,并且无法弄清楚原因的人:我自己找到了解决方案。我没有完全意识到要序列化为XML / JSON的POJO对象需要具有零参数构造函数。这包括原始对象中使用的集合。

For those who have the same sort of problem as I had, and can't figure out why: I found the solution myself. I wasn't fully aware of the fact that the POJO-objects that are to be serialized to XML/JSON need to have a zero-arguments constructor. That includes collections used in the original object.

所以在我的情况下:我的'ProductDetailsView'对象有一个'OnlineCompetitorView'对象列表。 ProductDetailsView和OnlineCompetitorView都需要具有零参数构造函数。对于一些人来说可能很明显,但我认为分享这些信息是明智的。我意外地在这篇链接,所以我想给予一些信任。

So in my case: my 'ProductDetailsView'-object has a list of 'OnlineCompetitorView'-objects. Both the ProductDetailsView and the OnlineCompetitorView need to have a zero-argument constructor. For some it might be obvious, but I thought it would be wise to share this information. I found this information, accidently, in the opening post in this link, so I want to give some credit to that.

这篇关于Jackson JSON生成HTTP状态500,XML工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆