本文介绍了Spring RestTemplate with Jackson抛出“无法解析BeanPropertyFilter”使用@JsonFilter时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以指定Spring的 RestTemplate 使用Jackson ObjectMapper 吗?

Can I specify the Jackson ObjectMapper that Spring's RestTemplate uses?

我不是100%这就是我需要做的事情,但请参阅下面的详细信息。

I'm not 100% that's what I need to do but see below for details.

背景:
在的帮助下,我添加了@ JsonFilter到我的域类并编辑了我的jax-rs Web服务(在CXF中实现)。我现在能够成功地动态选择在RESTful API中返回哪些域类字段。到目前为止一直很好。

Background:With help from this StackOverflow post I added @JsonFilter to my domain class and edited my jax-rs web service (implemented in CXF). I'm now successfully able to dynamically select which domain class fields to return in my RESTful API. So far so good.

我在JUnit测试中使用Spring的RestTemplate来测试我的RESTful API。这很好用,直到我将@JasonFilter添加到我的域类中。现在我得到以下异常:

I'm using Spring's RestTemplate in my JUnit tests to test my RESTful API. This was working fine until I added @JasonFilter to my domain class. Now I'm getting the following exception:

为了简洁省略了剩余的堆栈跟踪

rest of stack trace omitted for brevity

引起:org.codehaus.jackson.map.JsonMappingException:无法解析ID为'apiFilter'的BeanPropertyFilter;在org.codehaus.jackson.map.ser.BeanSerializer.findFilter(BeanSerializer.java:252)中没有配置FilterProvider

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not resolve BeanPropertyFilter with id 'apiFilter'; no FilterProvider configured

我在服务器端遇到了类似的问题,并且能够解决它(在)给杰克逊 ObjectMapper 过滤器提供 如下:

I was getting a similar problem on the server side and was able to resolve it (with help from this post) by giving a FilterProvider to the Jackson ObjectMapper as follows:

ObjectMapper mapper = new ObjectMapper();
FilterProvider filters = new SimpleFilterProvider().addFilter("apiFilter", SimpleBeanPropertyFilter.filterOutAllExcept(filterProperties));

我可以在RestTemplate方面做类似的事吗?任何关于如何解决这个问题的想法都值得赞赏。

Can I do something similar on the RestTemplate side? Any ideas of how to solve this issue are appreciated.

为了清楚起见,在客户端RestTemplate方面,我根本不想过滤域对象属性。

Just to be clear, on the client RestTemplate side I do not want to filter the domain object properties at all.

推荐答案

通过执行以下操作,我可以强制RestTemplate使用自定义的ObjectMapper:

I was able to force RestTemplate to use a customized ObjectMapper by doing the following:

ObjectMapper mapper = new ObjectMapper();

// set a custom filter
Set<String> filterProperties = new HashSet<String>();
FilterProvider filters = new SimpleFilterProvider().addFilter("apiFilter", SimpleBeanPropertyFilter.serializeAllExcept(filterProperties));
mapper.setFilters(filters);

MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter();
messageConverter.setObjectMapper(mapper);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
restTemplate.setMessageConverters(messageConverters);

提供了部分上述代码的示例。

This website provided example for part of the above code.

这篇关于Spring RestTemplate with Jackson抛出“无法解析BeanPropertyFilter”使用@JsonFilter时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:18