Jersey: list of JSON objects

28,999

Solution 1

This is what should work:

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

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

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.

Solution 2

Why you don't use a simple Java array???

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

And then the following method:

@POST
@Path("/send")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String sendEmails(AnyEmail[] emails) {
    //emailManager.sendEmail(emails);
    return "success";
}

That should do the trick...

Solution 3

I'm neither a Jersey nor Json expert, but did you try using a full JSON object, ie :

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

(added braces, and quotes to emails).

Share:
28,999

Related videos on Youtube

Dejell
Author by

Dejell

I like traveling around the world. So far I have been to: USA England Italy Slovania Croatia Jordan South Africa Zimbabwe Botswana France Canada Israel Thailand Switzerland Holland Bulgaria I am going to Vietnam soon

Updated on July 09, 2022

Comments

  • Dejell
    Dejell almost 2 years

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

    I have the @XmlRootElement above `AnyEmail.

    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"}
          ]
    

    I get:

    <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"}
              ]
    

    and I get:

        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)
    
  • SCO
    SCO over 12 years
    This sounds much better. "Bad Request" is a expectable return code. I'd dig into typing here, eg AnyEmail is mappable onto {"body": "", "header": "", "footer": ""} ? No additional idea ;)
  • ivan.cikic
    ivan.cikic over 12 years
    This is consumable format in case of mapped JSON notation, right? If natural is used, just passing an JSON array should work?