下面列出了com.fasterxml.jackson.databind.ObjectMapper#addHandler ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@PostConstruct
public void init() {
JacksonMapper nitriteMapper = new JacksonMapper();
ObjectMapper mapper = nitriteMapper.getObjectMapper();
mapper.addHandler(new UnknownPluginHandler());
db = CompletableFuture.supplyAsync(() -> createOrOpenDb(nitriteMapper));
}
private GoogleGroupIndex readGoogleGroupIndex(final String group, final String url, int connectTimeout, int readTimeout) {
try {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
setTimeout(restTemplate, connectTimeout, readTimeout);
ObjectMapper mapper = Jackson2ObjectMapperBuilder.xml().build();
mapper = mapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException {
if (beanOrClass instanceof GoogleGroupIndex) {
if ("versions".equals(propertyName)) {
if (((GoogleGroupIndex) beanOrClass).lastArtifactId != null) {
((GoogleGroupIndex) beanOrClass).downloaded.put(((GoogleGroupIndex) beanOrClass).lastArtifactId, p.getText());
((GoogleGroupIndex) beanOrClass).lastArtifactId = null;
}
} else {
((GoogleGroupIndex) beanOrClass).lastArtifactId = propertyName;
}
return true;
}
return false;
}
});
restTemplate.getMessageConverters().clear();
restTemplate.getMessageConverters().add(new MappingJackson2XmlHttpMessageConverter(mapper));
ResponseEntity<GoogleGroupIndex> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(requestHeaders), GoogleGroupIndex.class);
GoogleGroupIndex groupIndex = response.getBody();
groupIndex.group = group;
groupIndex.url = url;
return groupIndex.build();
} catch (Exception exception) {
Exceptions.printStackTrace(exception);
}
return null;
}
private GoogleMasterIndex readGoogleMasterIndex(int connectTimeout, int readTimeout) {
try {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
setTimeout(restTemplate, connectTimeout, readTimeout);
ObjectMapper mapper = Jackson2ObjectMapperBuilder.xml().build();
mapper = mapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException {
if (beanOrClass instanceof GoogleMasterIndex) {
((GoogleMasterIndex) beanOrClass).getGroups().put(propertyName, BASE_URL + propertyName.replace(".", "/") + "/" + GROUP_INDEX);
return true;
}
return false;
}
});
restTemplate.getMessageConverters().clear();
restTemplate.getMessageConverters().add(new MappingJackson2XmlHttpMessageConverter(mapper));
ResponseEntity<GoogleMasterIndex> response = restTemplate.exchange(BASE_URL + MASTER_INDEX, HttpMethod.GET, new HttpEntity<>(requestHeaders), GoogleMasterIndex.class);
return response.getBody();
} catch (Exception exception) {
Exceptions.printStackTrace(exception);
}
return null;
}
/**
* Perform a default configuration of the given object mapper for
* parsing glTF data
*
* @param objectMapper The object mapper
* @param jsonErrorConsumer The consumer for {@link JsonError}s. If this
* is <code>null</code>, then the errors will not be handled.
*/
public static void configure(
ObjectMapper objectMapper,
Consumer<? super JsonError> jsonErrorConsumer)
{
// Some glTF files have single values instead of arrays,
// so accept this for compatibility reasons
objectMapper.configure(
DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.addHandler(
createDeserializationProblemHandler(jsonErrorConsumer));
// Register the module that will initialize the setup context
// with the error handling bean deserializer modifier
objectMapper.registerModule(new SimpleModule()
{
/**
* Serial UID
*/
private static final long serialVersionUID = 1L;
@Override
public void setupModule(SetupContext context)
{
super.setupModule(context);
context.addBeanDeserializerModifier(
createErrorHandlingBeanDeserializerModifier(
jsonErrorConsumer));
}
});
}
/**
* Creates the ObjectMapper that serializes json to entity. Wraps the root (most often "data").
*
* See @JsonRootName on the RestEntities
*
* @return
*/
private ObjectMapper createObjectMapperWithRootUnWrap() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
mapper.addHandler(new CustomDeserializationProblemHandler());
mapper.registerModule(new JodaModule());
return mapper;
}
private ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.addHandler(new UnknownPluginHandler());
return mapper;
}
private ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.addHandler(new UnknownPluginHandler());
mapper.enable(SerializationFeature.INDENT_OUTPUT); //allows git line-by-line diffs
return mapper;
}
private ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.addHandler(new UnknownPluginHandler());
return mapper;
}
private ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.addHandler(new UnknownPluginHandler());
return mapper;
}
private ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.addHandler(new UnknownPluginHandler());
return mapper;
}
private ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.addHandler(new UnknownPluginHandler());
return mapper;
}