spring-restdocs icon indicating copy to clipboard operation
spring-restdocs copied to clipboard

Make it easier to use JSON libraries other than Jackson alongside REST Docs

Open BK-Choi opened this issue 9 years ago • 4 comments

My spring project works fine, but tests crashes when I add spring-restdocs to gradle dependencies. Just that (no configuration chages, no test code changes) brakes my project.

The problem is that spring-restdocs library make spring framework ignore mvc:message-converters configuration. (I'm not sure what is exactly happened) I added gson over jackson for JSON as you can see below. but with spring-restdocs, only I see is messageConverters without GsonHttpMessageConverter.

I have configuration like this: dispatcher-servlet.xml (there are applicationContext.xml and applicationContext-rest.xml as well)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<!-- message converter -->
    <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
        <mvc:message-converters>
            <ref bean="jaxbMessageConverter" />
            <ref bean="gsonMessageConverter" />
        </mvc:message-converters>
    </mvc:annotation-driven>

    <bean id="gsonMessageConverter" class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>
    <bean id="jaxbMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <property name="marshaller" ref="jaxbMarshaller" />
        <property name="unmarshaller" ref="jaxbUnmarshaller" />
        <property name="supportedMediaTypes">
            <list>
                <value>application/xml</value>
                <value>application/xml;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="packagesToScan">
            <list>
                <value>foo.boo.**.output</value>
            </list>
        </property>
    </bean>
    <bean id="jaxbUnmarshaller" name="unmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="packagesToScan">
            <list>
                <value>foo.boo.**.model</value>
            </list>
        </property>
    </bean>
</beans>

gradle

dependencies {
    testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:1.1.0.RELEASE'
}

BK-Choi avatar Jun 17 '16 08:06 BK-Choi

The problem could be that spring-restdocs-core has a compile dependency on jackson-databind. That might make some auto-detection logic in Spring MVC try to use Jackson rather than GSON. However, looking at your configuration, I can't see why that would be the case.

Can you please provide a complete sample that reproduces the problem so that I can dig into it a bit more?

wilkinsona avatar Jun 17 '16 08:06 wilkinsona

@wilkinsona, sorry for late created sample project cause the "my project" cannot be opened. you can see the priority-changing-problem when you uncomment the last line of build.gradle file.

https://github.com/BK-Choi/spring-restdocs-with-gson

BK-Choi avatar Jun 17 '16 09:06 BK-Choi

Thanks for the sample. The problem's in your configuration of your RestTemplate bean. Its configuration doesn't specify its message converters so, when Jackson is on the classpath, a Jackson-based converter for JSON is preferred to GSON. You can avoid the problem by explicitly configuring its converters:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate" >
    <constructor-arg>
        <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory" >
            <property name="readTimeout" value="1000"/>
            <property name="connectTimeout" value="1000"/>
        </bean>
    </constructor-arg>
    <property name="messageConverters">
        <list>
            <ref bean="gsonMessageConverter"/>
        </list>
    </property>
</bean>

It would be nice if this wasn't necessary, though. I'll explore the possibilty of removing the Jackson dependency from spring-restdocs-core.

wilkinsona avatar Jun 17 '16 10:06 wilkinsona

I think just as you do. this solution and your to-do think. Thank you, really.

BK-Choi avatar Jun 17 '16 11:06 BK-Choi