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

猜你喜欢

转载自blog.csdn.net/lvyuan1234/article/details/80119353