Jersey: JSON Object List

I am trying to get the following objects in my jersey resource resource resource collection:

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

I have @XmlRootElement above `AnyEmail.

However, when I submit this using the 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) 
+6
source share
6 answers

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

Alternatively, you can use the POJO approach, which is preferred for JSON - see here: https://jersey.java.net/documentation/1.18/json.html

JAXB-based JSON support has various problems with some edge cases, since there is no 1: 1 mapping between XML (JAXB as provided) and JSON.

+4
source

Why aren't you using 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 ...

+7
source

I am not an expert in Jersey and Jones, but have you tried using the full JSON object i.e.

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

(curly braces and quotation marks added to email messages).

+1
source

I had the same problem. You must wrap the AnyEmail class.

This may help you: https://blogs.oracle.com/japod/entry/missing_brackets_at_json_one

0
source

//Client

 package com.project.rest.model; import java.util.HashSet; import java.util.Set; public class Client { private Long id; private String email; private String lang; public Client() { } public Client(Long id) { this.id = id; } public Client(Long id, String email, String lang) { this.id = id; this.email = email; this.lang = lang; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } @Override public String toString() { return "Client [id=" + id + ", email=" + email + ", lang=" + lang + "]"; } } 

// ClientService

 package com.project.rest; import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.project.rest.model.Client; @Path("/client") public class ClientService { @POST @Path("/sendList") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response consumeJSONList(List<Client> clientList) { String output = "consumeJSONList Client : " + clientList.toString() + "\n\n"; return Response.status(200).entity(output).build(); } } 

// jerseyclient

 package com.project.rest; import java.util.ArrayList; import java.util.List; import javax.ws.rs.core.MediaType; import com.project.rest.model.Client; import com.project.rest.model.Device; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.json.JSONConfiguration; public class JerseyClient { public static void main(String[] args) { try { List<Client> clientList = new ArrayList<Client>(); clientList.add(new Client(1L, " pruebas@pruebas.com ", "es")); clientList.add(new Client(2L, " pruebas@pruebas.com ", "es")); clientList.add(new Client(3L, " pruebas@pruebas.com ", "es")); ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); com.sun.jersey.api.client.Client c = com.sun.jersey.api.client.Client.create(clientConfig); WebResource webResource = c.resource("http://localhost:8080/project_rest/rest/client/sendList"); ClientResponse response = webResource.accept("application/json").type("application/json").post(ClientResponse.class, clientList); if (response.getStatus() != 200) { throw new RuntimeException("Failed sendClientList: HTTP error code : " + response.getStatus()); } String output = response.getEntity(String.class); System.out.println("sendClientList... Server response .... \n"); System.out.println(output); } catch (Exception e) { e.printStackTrace(); } } } 

//pom.xml

 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-bundle</artifactId> <version>1.10-b01</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.17.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.17.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.17.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.18.1</version> </dependency> <dependency> <groupId>com.owlike</groupId> <artifactId>genson</artifactId> <version>0.99</version> </dependency> 

0
source

I am using Jersey 1.17 and I can get a list of JSON objects with the following signature and transfer them to my list of POJO objects using Google gson library without any problems. Using a wrapper to wrap a list of POJO objects is certainly unsuitable and not profitable.

  @POST @Consumes(MediaType.APPLICATION_JSON) @Produces({"application/x-javascript", MediaType.APPLICATION_JSON}) public Response doSomething(@Context HttpServletRequest httpServletRequest, JSONArray listOfJSONObjects) { Gson gson = new GsonBuilder().create(); List < MYPOJOClass > myPojoObjectList = gson.fromJson(listOfJSONObjects.toString(), new TypeToken < List < MYPOJOClass >> () {}.getType()); 
0
source

Source: https://habr.com/ru/post/898593/


All Articles