JSON returns incorrectly with Spring MVC 3.2 Deferred result

I am trying to use Spring MVC 3.2 asynchronous deferred results for a RESTful JSON service.

Using a synchronous approach:

@ResponseBody
@RequestMapping(value = "/catalog", method = RequestMethod.GET, produces = "application/json")
public Entry catalog() {
  Entry entry = new Entry();
  entry.timestamp = System.currentTimeMillis();
  entry.summary = "Hello World!";
  entry.body = new HashMap<String, Object>();
  entry.body.put("key1", "value1");
  entry.body.put("key2", "value2");
  entry.body.put("key3", "value3");
  return entry;
}

      

I get the following JSON result:

{"timestamp":1359721240340,"summary":"Hello World!","body":{"key3":"value3","key2":"value2","key1":"value1"}}

      

Using an asynchronous approach (contrived example):

@ResponseBody
@RequestMapping(value = "/catalogs", method = RequestMethod.GET, produces = "application/json")
public DeferredResult<Entry> catalogs() {
    Entry entry = new Entry();
    entry.timestamp = System.currentTimeMillis();
    entry.summary = "Hello World!";
    entry.body = new HashMap<String, Object>();
    entry.body.put("key1", "value1");
    entry.body.put("key2", "value2");
    entry.body.put("key3", "value3");
    DeferredResult<Entry> result = new DeferredResult<Entry>();
    result.setResult(entry);
    return result;
}

      

I get the following:

{}{"timestamp":1359721240340,"summary":"Hello World!","body":{"key3":"value3","key2":"value2","key1":"value1"}}

      

So what's the deal with the empty object prefix (i.e. {})? Am I doing something clearly stupid or MappingJackson2JsonView

not playing well with the new Async features?

FYI here is my Spring MVC bean config:

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="contentNegotiationManager">
        <bean class="org.springframework.web.accept.ContentNegotiationManager" />
    </property>
    <property name="defaultViews">
        <list>
            <bean
                class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
        </list>
    </property>
</bean>

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="2" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

      

+3


source to share


1 answer


I really wanted to comment, not "reply", but I can't do that yet. I am getting the same results as you. MappingJackson2JsonView

doesn't seem to work with DeferredResult. Moreover, as soon as I upgraded to Spring 3.2.3, the resulting JSON was appended with {} && {}, although I had in my configuration:<property name="prefixJson" value="false" />

My initial attempt to fix this involved recording HttpServletRequestWrapper

and filtering from which I could peel off the results, but even that doesn't seem to play well in the land of Async Servlet.



The only approach I've found to work for my particular application is to filter out the extra junk with JavaScript. While it works, it makes me want to kill myself, or at least pull out a nail or two.

I know this is not very helpful. I hope you find comfort in the fact that I, too, cry over this seemingly simple pain in the balls.

+1


source







All Articles