json Spring 3.1.2 中的注册器 MappingJackson2HttpMessageConverter 带有 JAXB 注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12514285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:32:44  来源:igfitidea点击:

Registrer MappingHymanson2HttpMessageConverter in Spring 3.1.2 with JAXB annotations

jsonspringjaxbmockingHymanson

提问by molholm

I have a number of entities with JAXB annotations that I would like to convert to JSON using a message-converter.

我有许多带有 JAXB 注释的实体,我想使用消息转换器将它们转换为 JSON。

I know that my ObjectMapper that reads the JAXB annotations works:

我知道读取 JAXB 注释的 ObjectMapper 有效:

String correctJsonText = HymansonObjectMapper.writeValueAsString(entityWithJAXB); 

But when i call my rest service the default registered MappingHymansonHttpMessageConverter (which is not configured for reading JAXB) seems to take over - resulting in a stackoverflow due to cyclic references when @XmlTransient is ignored...

但是当我调用我的休息服务时,默认注册的 MappingHymansonHttpMessageConverter(它没有被配置为读取 JAXB)似乎接管了 - 当@XmlTransient 被忽略时,由于循环引用导致堆栈溢出......

How do i configure Spring to use MappingHymanson2HttpMessageConverter ?

我如何配置 Spring 以使用 MappingHymanson2HttpMessageConverter ?

Current Configuration

当前配置

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="false">
        <bean id="HymansonMessageConverter" class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
            <property name="objectMapper" ref="HymansonObjectMapper" />
            <property name="supportedMediaTypes">
                <list>
                    <bean class="org.springframework.http.MediaType">
                        <constructor-arg index="0" value="application" />
                        <constructor-arg index="1" value="json" />
                        <constructor-arg index="2" value="UTF-8" />
                    </bean>
                </list>
            </property>

        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<bean id="jaxbAnnotationInspector" class="com.fasterxml.Hymanson.module.jaxb.JaxbAnnotationIntrospector" />

<bean id="HymansonObjectMapper" class="com.fasterxml.Hymanson.databind.ObjectMapper">
    <property name="annotationIntrospector" ref="jaxbAnnotationInspector" />
</bean>

REST service

休息服务

@RequestMapping(value="/{id}", method=RequestMethod.GET, produces = "application/json;charset=UTF-8")
public @ResponseBody EntityWithJAXB readEntityWithJAXB(@PathVariable int id, Model model) {
    return entityService.getById(id);
}

Dependencies

依赖关系

<dependency>
    <groupId>com.fasterxml.Hymanson.core</groupId>
    <artifactId>Hymanson-core</artifactId>
    <version>2.0.5</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.Hymanson.core</groupId>
    <artifactId>Hymanson-databind</artifactId>
    <version>2.0.5</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.Hymanson.module</groupId>
    <artifactId>Hymanson-module-jaxb-annotations</artifactId>
    <version>2.0.5</version>
</dependency>

UPDATE / Solution

更新/解决方案

By debugging my context I could see that the configuration in <mvc:annotation-driven>had no effect.

通过调试我的上下文,我可以看到配置<mvc:annotation-driven>无效。

It turned out that my unit test with MockMcv always loaded the default handleradapters, thus ignoring my custom objectmapper. Due to convenience I only tested the controller using junit test, since it hit the controller just fine I did not think of this as a probable error cause...

事实证明,我使用 MockMcv 的单元测试总是加载默认的handleradapters,从而忽略了我的自定义对象映射器。由于方便,我只使用 junit test 测试了控制器,因为它很好地击中了控制器,我不认为这是一个可能的错误原因......

I did not find a fix for my test yet, but when I call the service using curl everything works!

我还没有找到适合我的测试的修复程序,但是当我使用 curl 调用该服务时,一切正常!

UPDATE / Final solution

更新/最终解决方案

Just found a solution for my test setup; when using MockMvc (spring-test-mvc) you must specify custom message-converters explicit:

刚刚为我的测试设置找到了解决方案;使用 MockMvc (spring-test-mvc) 时,您必须明确指定自定义消息转换器:

private MockMvc mockMvc;

@Autowired
private MappingHymanson2HttpMessageConverter HymansonMessageConverter;

@Autowired
private RestController restController;

@Before
public void initMockMvc(){
    mockMvc = MockMvcBuilders.standaloneSetup(restController)
            .setMessageConverters(HymansonMessageConverter).build();
} 

@Test
public void testRestController() throws Exception{
    DefaultRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/json/42");
    this.mockMvc.perform(requestBuilder).andDo(print()).andExpect(status().isOk());
}

The only remaining issue is that the HymansonMessageConvertercan't be autowired directly until the following JIRA is resolved: https://jira.springsource.org/browse/SPR-9469. Until then, I have just created a copy of the HymansonMessageConverter in my test context.

唯一剩下的问题是在HymansonMessageConverter解决以下 JIRA 之前无法直接自动装配:https: //jira.springsource.org/browse/SPR-9469。在那之前,我刚刚在我的测试上下文中创建了 HymansonMessageConverter 的副本。

采纳答案by Biju Kunjummen

I replicated your configuration and it works perfectly for me. The default message converters should not take effect, as you have explicitly specified the register-defaults=falseattribute. This is how my @XmlTransient annotation looks:

我复制了您的配置,它非常适合我。默认消息转换器不应生效,因为您已明确指定了该register-defaults=false属性。这是我的@XmlTransient 注释的样子:

@XmlAccessorType(XmlAccessType.FIELD)
public class EntityWithJAXB {


    @XmlTransient
    private EntityWithJAXB aChild;