Note that there are some explanatory texts on larger screens.

plurals
  1. POCan not deserialize instance of java.util.ArrayList out of VALUE_STRING
    text
    copied!<p>I have a REST service built with Jersey and deployed in the AppEngine. The REST service implements the verb PUT that consumes an application/json media type. The data binding is performed by Jackson.</p> <p>The verb consumes an enterprise-departments relation represented in JSON as </p> <pre><code>{"name":"myEnterprise", "departments":["HR","IT","SC"]} </code></pre> <p>In the client side, I use gson to convert the JSON representation into a java object. Then, I pass the object to my REST service and it works fine.</p> <p><strong>Problem:</strong></p> <p>When my JSON representation has only one item in the collection</p> <pre><code>{"name":"myEnterprise", "departments":["HR"]} </code></pre> <p>the service cannot deserialize the object.</p> <pre><code>ATTENTION: /enterprise/enterprise: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token at [Source: org.mortbay.jetty.HttpParser$Input@5a9c5842; line: 1, column: 2 </code></pre> <p>As reported by other users, the solution is to add the flag ACCEPT_SINGLE_VALUE_AS_ARRAY (e.g., <a href="https://stackoverflow.com/questions/14319573/jersey-can-not-deserialize-instance-of-arraylist-out-of-string">Jersey: Can not deserialize instance of ArrayList out of String</a>). Nevertheless, I am not controlling an ObjectMapper because in the service side it is transparently made by Jackson.</p> <p><strong>Question:</strong></p> <p>Is there a way to configure the ObjectMapper on the service side to enable ACCEPT_SINGLE_VALUE_AS_ARRAY? annotations? web.xml?</p> <p><strong>Code details</strong></p> <p>Java object:</p> <pre><code>@XmlRootElement public class Enterprise { private String name; private List&lt;String&gt; departments; public Enterprise() {} public String getName() { return name; } public void setName(String name) { this.name = name; } public List&lt;String&gt; getDepartments() { return departments; } public void setDepartments(List&lt;String&gt; departments) { this.departments = departments; } } </code></pre> <p>The REST service side:</p> <pre><code> @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/enterprise") public Response putEnterprise(Enterprise enterprise, @Context HttpServletRequest req){ ... } </code></pre> <p>Client side:</p> <pre><code>... String jsonString = "{\"name\":\"myEnterprise\", \"departments\":[\"HR\"]}"; Enterprise enterprise = gson.fromJson(jsonString, Enterprise.class); System.out.println(gson.toJson(enterprise)); response = webResource .type(MediaType.APPLICATION_JSON) .put(ClientResponse.class,enterprise); if (response.getStatus() &gt;= 400) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } ... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload