Deserialize ArrayList from String using Jackson

前端 未结 2 1758
借酒劲吻你
借酒劲吻你 2020-12-11 06:38

I am using Spring\'s MappingJacksonHttpMessageConverter to convert JSON message to object in my controller.



        
相关标签:
2条回答
  • 2020-12-11 07:04

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

    https://github.com/FasterXML/jackson-dataformat-xml/issues/21

    For me adding the following solved this issue

    jsonMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    
    0 讨论(0)
  • 2020-12-11 07:08

    As far as I see the incoming JSON doesn't contain any array. The question is: is "images" supposed to be separated or it contains a single image? Let's assume they are comma separated:

    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;
       }
    }
    
    0 讨论(0)
提交回复
热议问题