Sabya
Sabya

Reputation: 207

Jackon custom deserializer never called

This is the attribute on which I have used the @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;
}

And the custom Deserializer code is

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<>();
}
}

But the deserializer is never called. Please help

Everytime I get this exception instead....

 ERROR :::9,[email protected] - 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]':

Please help........

Upvotes: 0

Views: 3291

Answers (2)

Barath
Barath

Reputation: 5283

Have you register the module as mentioned in this example http://www.baeldung.com/jackson-deserialization ?

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

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

It is working for me :

    @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>>() {
    });

}

Model :

public class TestUser {

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

public class UserVehicleMappingVO {

    private String name;

//getters and setters 

}

Custom Deserializer class :

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<>();
}

Response :

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

Customize the 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;
}

Reference here : https://dzone.com/articles/customizing

Upvotes: 1

John Tribe
John Tribe

Reputation: 1622

Try deserialize only "Set" -> extends JsonDeserializer< Set >

Upvotes: 0

Related Questions