user2122264
user2122264

Reputation: 163

How to enable @JsonRootName in spring mvc 3.2

I'm using Spring 3.2 and my Spring MVC controller generate JSON data (with jackson-databind-2.2.0). I would like to customize my JSON root name with @JsonRootName (com.fasterxml.jackson.annotation.JsonRootName) annotation, however, I could not figure out how to enable it with Spring configuration.

@JsonRootName("rootNameTest")
public class MyModel {
    private String prop;
    public String getProp() {
        return prop;
    }
    public void setProp(String prop) {
        this.prop = prop;
    }
}

Here's my settings in sevlet-context.xml

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="contentNegotiationManager">
        <bean class="org.springframework.web.accept.ContentNegotiationManager">
            <constructor-arg>
                <bean class="org.springframework.web.accept.ParameterContentNegotiationStrategy">
                    <constructor-arg>
                        <map>
                            <entry key="json" value="application/json"/>
                        </map>
                    </constructor-arg>
                </bean>
            </constructor-arg>
        </bean>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
        </list>
    </property>
</bean>

Please help. Thanks.

Upvotes: 2

Views: 2853

Answers (1)

Spatil
Spatil

Reputation: 31

Setbelow in com.fasterxml.jackson.databind.ObjectMapper

om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); om.configure(SerializationFeature.WRAP_ROOT_VALUE, true);

this may be done by extending above Class with your custom and inject in org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

Upvotes: 3

Related Questions