Chetan Shirke
Chetan Shirke

Reputation: 906

How to customize spring 3.0 json response?

I am using spring 3.0 webservices. It produces a json response.

I want to generate jsonp response using same webservices.

Please suggest how to customize spring 3.0 webservices json response.

Upvotes: 1

Views: 368

Answers (1)

Ivaylo Kovatchev
Ivaylo Kovatchev

Reputation: 68

You can try to integrate Jackson in your project, which has tons of options for JSON serialization. Configuring Jackson is easy:

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="0" />
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <bean
                class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
    </property>
</bean>

Once you get this working, you can use Jackson's excellent annotations on your Model objects to configure how they get printed in the JSON, or if they get ignored (@JSONIgnore).

If you want to go further, you can extend the org.springframework.web.servlet.view.json.MappingJacksonJsonView class with your own, and go nuts.

Hope this helps.

Upvotes: 1

Related Questions