Jackon自定义反序列化器从未调用

时间:2017-08-10 14:32:02

标签: java spring jackson json-deserialization objectmapper

这是我使用@JsonDeserialize

的属性
@Transient
@JsonDeserialize(using = SharedUserDeserializer.class)
private Set<UserVehicleMappingVO> sharedVehicle;

public Set<UserVehicleMappingVO> getSharedVehicle() {
    return sharedVehicle;
}
public void setSharedVehicle(Set<UserVehicleMappingVO> sharedVehicle) {
    this.sharedVehicle = sharedVehicle;
}

自定义反序列化代码是

public class SharedUserDeserializer extends JsonDeserializer<Set<UserVehicleMappingVO>> {

@Override
public Set<UserVehicleMappingVO> deserialize(JsonParser paramJsonParser,
        DeserializationContext paramDeserializationContext)
        throws IOException, JsonProcessingException {
    try {
        Set<UserVehicleMappingVO> list = new ObjectMapper().readValue(paramJsonParser.toString(),
                new TypeReference<Set<UserVehicleMappingVO>>() {});
        return list;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new HashSet<>();
}
}

但是反序列化器从未被调用过。请帮忙

每次我得到这个例外......

 ERROR :::9,gajendranc@azuga.com - Exception in 
method===org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'trackee' on field 'sharedVehicle': rejected value 
[[{"userId":"5d48b74f-7da2-11e7-87bf-
1383429d1d89","expireTime":1504190100000}]]; codes 
 [typeMismatch.trackee.sharedVehicle,typeMismatch.sharedVehicle,
typeMismatch.java.util.Set,typeMismatch]; arguments 
[org.springframework.context.support.DefaultMessageSourceResolvable: codes 
[trackee.sharedVehicle,sharedVehicle]; arguments []; default message 
[sharedVehicle]]; default message [Failed to convert property value of type 
[java.lang.String] to required type [java.util.Set] for property 
'sharedVehicle'; nested exception is java.lang.IllegalStateException: Cannot 
convert value of type [java.lang.String] to required type 
[com.azuga.user.manager.UserVehicleMappingVO] for property 
'sharedVehicle[0]':

请帮助........

2 个答案:

答案 0 :(得分:0)

仅尝试反序列化&#34;设置&#34; - &GT;扩展JsonDeserializer&lt;设置&gt;

答案 1 :(得分:0)

您是否按照此示例http://www.baeldung.com/jackson-deserialization中所述注册了模块?

        ObjectMapper mapper=new ObjectMapper();
        SimpleModule module = new SimpleModule();

        module.addDeserializer(Set.class, new SharedUserDeserializer());
        mapper.registerModule(module);

这对我有用:

    @Test
    public void test() throws IOException {

        ObjectMapper mapper=new ObjectMapper();
        SimpleModule module = new SimpleModule();

        module.addDeserializer(Set.class, new SharedUserDeserializer());
        mapper.registerModule(module);
       TestUser user=new TestUser();

    Set<UserVehicleMappingVO> sets=new HashSet<>();

    sets.add(new UserVehicleMappingVO("test1"));
    user.setVechicles(sets);

    String jsonString=mapper.writeValueAsString(user);
    Set<UserVehicleMappingVO> vechiles=mapper.readValue(jsonString, new TypeReference<Set<UserVehicleMappingVO>>() {
    });

}

型号:

public class TestUser {

    @JsonDeserialize(using = SharedUserDeserializer.class)
    private Set<UserVehicleMappingVO> vechicles;
   //getters and setters 
}

public class UserVehicleMappingVO {

    private String name;

//getters and setters 

}

自定义反序列化程序类:

public class SharedUserDeserializer extends JsonDeserializer<Set<UserVehicleMappingVO>> {

@Override
public Set<UserVehicleMappingVO> deserialize(JsonParser paramJsonParser,
        DeserializationContext paramDeserializationContext)
        throws IOException, JsonProcessingException {
    try {
        System.out.println("hello");
        Set<UserVehicleMappingVO> list = new ObjectMapper().readValue(paramJsonParser.toString(),
                new TypeReference<Set<UserVehicleMappingVO>>() {});
        return list;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new HashSet<>();
}

回应:

Output {"vechicles":[{"name":"test1"}]}
hello

自定义HttpMessageConverters:

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
 MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
 ObjectMapper objectMapper = new ObjectMapper();
 SimpleModule module = new SimpleModule();

            module.addDeserializer(Set.class, new SharedUserDeserializer());
            objectMapper.registerModule(module);

 jsonConverter.setObjectMapper(objectMapper);
 return jsonConverter;
}

参考此处:https://dzone.com/articles/customizing