MappingJacksonJsonView returns top level json object

I converted to a controller to use ContentNegotiationViewResolver instead of MessageConverters to support several types of output. Using json I use MappingJacksonJsonView:

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="mediaTypes">
        <map>
            <entry key="html" value="text/html"/>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <constructor-arg>
                    <bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
                </constructor-arg>
            </bean>                         
        </list>
    </property>
    <property name="ignoreAcceptHeader" value="true" />
    <property name="defaultContentType" value="application/json" />
</bean>

With the following controller logic:

@RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
public ModelAndView getById(@PathVariable (value="id") String id) {
    MyObject ret = doGetById(id);
    ModelAndView modelAndView = new ModelAndView("common/single");
    modelAndView.addObject("myObject", ret);
    return modelAndView;
}

Returning json when accessing / id / 1234.json is something like:

{
   myObject: {
        field1:"abc",
        field2:"efg"
   }
}

Is there a way to set myObject as the top level node for the result so that it looks like this:

{
    field1:"abc",
    field2:"efg"
}
+5
source share
3 answers

Spring MVC ModelAndView JSON. ModelAndView , myObject, JSON. , ModelAndView Jackson JSON.

, ModelAndView, MyObject @ResponseBody,

@RequestMapping(value="/id/{id}", method=RequestMethod.GET, produces="application/json")
public @ResponeBody MyObject getById(@PathVariable (value="id") String id) {
    return doGetById(id);
}
+8

, .

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
   <property name="prefixJson" value="true" />
</bean>
+2

You can remove the external node with MappingJacksonJsonView.setExtractValueFromSingleKeyModel(true):

Specify whether to serialize models containing one attribute as a map or extract a single value from a model and serialize it directly.

The effect of setting this flag is similar to the effect of the MappingJacksonHttpMessageConverter with the @ResponseBody request processing method.

For instance:

private final MappingJacksonJsonView view = new MappingJacksonJsonView();

public MyController() {
     view.setExtractValueFromSingleKeyModel(true);
}

@RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
public ModelAndView getById(@PathVariable (value="id") String id) {
    MyObject ret = doGetById(id);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setView(this.view);
    modelAndView.addObject("myObject", ret);
    return modelAndView;
}

This should also work if you prefer to do this through configuration:

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="extractValueFromSingleKeyModel" value="true" />
</bean>
+2
source

All Articles