English 中文(简体)
Deserialize ArrayList from String using Jackson
原标题:Deserialize ArrayList from String using Jackson

我正在利用春天绘图JacksonHttpMessageConverter将JSON电文改为我的控制器。

<bean id="jsonConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="prefixJson" value="false" />
    <property name="supportedMediaTypes" value="application/json" />
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter" />
        </list>
    </property>
</bean>

For fields that are declared as ArrayList, if the json message contains a String instead, the following exception will be thrown:

org.springframework.http.converter.HttpMessageNotReadableException: 
 Could not read JSON: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token

例子如下:

public class Product {
   private String name;
   private List<String> images;
}

在即将到来的Json:

{name:"Widget", images:"image1.jpg"}

你可以看到,这将产生例外,因为形象预计将是一个阵列。

我要使习惯的帝国更加宽容。 如果脱硫,就会产生从强令中独一无二的分子。 我将如何将这一点纳入测绘局(JacksonHttpMessageConverter)或目标申请机构?

我不希望利用通知标示每个ArrayList油田,这样就可以使用一种习惯。 我正在寻求一种办法,以压倒缺席的帝国,以预示这一功能。

最佳回答

Check out this article describing how to use the features of the jackson objectMapper to accomplish this.

请允许我补充说,这个问题已经解决了。

jsonMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
问题回答

就我看见即将到来的JSON没有一阵列。 问题是:是否应该分离“算术”或包含单一形象? 让我们假定,他们与他人分离:

public class Product {
   private String name;
   private List<String> images;

   @JsonProperty("images")
   public String getImagesAsString() {
      StringBuilder sb = new StringBuilder();
      for (String img : images) {
          if (sb.length() > 0) sb.append( , );
          sb.append(img);
      }
      return sb.toString();
   }

   public void setImagesAsString(String img) {
       this.images = Arrays.asList(img.split(","));
   }

   @JsonIgnore
   public List<String> getImages() {
       return images;
   }
}




相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...