Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token at

     我有如下一个接口,接口参数是一个实体类User,user实体包含email和List<tastes> 等属性

@RequestMapping(value = "/users", method = RequestMethod.POST)
public ResponseEntity<User> createUser(@RequestBody User user) {
public class User {
    private String email;
    private LocalDate birthDate;
    private List<String> tastes = new ArrayList<>();

 

     现在我想通过httpie命令行工具来测试接口(httpie类似于curl,但是他更加面向REST查询,它允许我们按照如下的方式输入命令  

http PUT http://localhost:8080/put hello=world

     我是在windows上操作的,当我输入以下命令测试接口时报错了:



     错误显示在传递tastes参数时出错了,因为tastes参数在Java类中是list类型,而httpie支持使用如下格式表示json类型:


    

     我不知道哪块出了问题,我想还是直接改成用等号吧,即把tastes:='["spring"]'改为tastes='["spring"]',这次报了新的错误

     

    网上说是因为序列化默认配置不会将单独一个字符串作为数组来映射,所以我们需要在配置类中开启允许单独字符串映射为数组,在objectmapper中添加一行代码:

@Configuration
@EnableSwagger2
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
        return objectMapper;
    }
}

 

   现再测试就OK了,不过有谁知道那个tastes:='["spring"]'这种格式为什么不行吗,知道的话麻烦告诉我一声

    

 参考地址:https://stackoverflow.com/questions/14588727/can-not-deserialize-instance-of-java-util-arraylist-out-of-value-string

              https://segmentfault.com/a/1190000012290989

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值