泽西岛:JSON对象列表 [英] Jersey: list of JSON objects

查看:129
本文介绍了泽西岛:JSON对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Jersey实施资源类中检索像这样的对象的集合:

I am trying to retrieve in my Jersey implementation resource class post collection of objects like this:

@POST
@Path("/send")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String sendEmails(ArrayList<AnyEmail> email) {
    //emailManager.sendEmail(email);
    return "success";
}

我在"AnyEmail"上方有@XmlRootElement.

I have the @XmlRootElement above `AnyEmail.

但是,当我使用REST客户端工具发布此类信息时:

However when I post like this with REST client tool:

 emails : [
       {"body": "Testing the web service", "header": "Hi", "footer": "<br/>test"},
       {"body": "Testing the web service", "header": "Hi", "footer": "<br/>test"}
      ]

我得到:

<html><head><title>Apache Tomcat/7.0.22 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server encountered an internal error () that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>javax.servlet.ServletException: Servlet execution threw an exception
</pre></p><p><b>root cause</b> <pre>java.lang.Error: Error: could not match input
    com.sun.jersey.json.impl.reader.JsonLexer.zzScanError(JsonLexer.java:491)
    com.sun.jersey.json.impl.reader.JsonLexer.yylex(JsonLexer.java:736)

已编辑
现在我尝试了:

EDITED
Now I tried:

 "emails" : [
           {"body": "Testing the web service", "header": "Hi", "footer": "<br/>test"},
           {"body": "Testing the web service", "header": "Hi", "footer": "<br/>test"}
          ]

我得到:

    SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/API] threw exception
java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.get(ArrayList.java:324)
    at com.sun.jersey.json.impl.reader.JsonXmlStreamReader.valueRead(JsonXmlStreamReader.java:165)
    at com.sun.jersey.json.impl.reader.JsonXmlStreamReader.readNext(JsonXmlStreamReader.java:330)

推荐答案

这应该起作用:

{
"anyEmail" : [
       {"body": "Testing the web service", "header": "Hi", "footer": "<br/>test"},
       {"body": "Testing the web service", "header": "Hi", "footer": "<br/>test"}
       ]
}

此外,您可能想使用POJO方法,这是JSON的首选方法-请参见此处: https://jersey.java.net/documentation/1.18/json.html

Also, you may want to use the POJO approach, which is the preferred one for JSON - see here: https://jersey.java.net/documentation/1.18/json.html

基于JAXB的JSON支持在某些极端情况下会遇到各种问题,因为XML(设计用于JAXB)与JSON之间没有1:1映射.

The JSON support based on JAXB has various issues with some edge cases, since there isn't a 1:1 mapping between XML (JAXB as designed for) and JSON.

这篇关于泽西岛:JSON对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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