gpt4 book ai didi

java - Jackon 自定义解串器从未调用过

转载 作者:行者123 更新时间:2023-12-02 12:23:30 24 4
gpt4 key购买 nike

这是我使用@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]':

请帮忙......

最佳答案

您是否已注册此示例中提到的模块 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

关于java - Jackon 自定义解串器从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45616600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com