OK, I have a spring mvc based json web service. This is a test app, I have never seen a problem like this when building spring mvc based restful json services. The output of my test service always returns and empty json object followed by && followed by the data i want to return. So the result looks like this:
{} && {"status":200,"serverTime":"January 6, 2013 7:35:45 PM EST"}
The code for my controller method to process this very simple GET request is:
@RequestMapping(value = "/test.json", method = RequestMethod.GET)
public ModelMap test(ModelMap m, HttpServletRequest request,
Locale locale) {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
m.addAttribute("serverTime", formattedDate);
m.addAttribute("status", 200);
return m;
}
I cant for the life of me figure out where that extra empty {} json object is coming from. my spring config looks like this:
<beans:bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<beans:property name="mediaTypes">
<beans:map>
<beans:entry key="html" value="text/html" />
<beans:entry key="json" value="application/json" />
</beans:map>
</beans:property>
<beans:property name="defaultViews">
<beans:list>
<beans:bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<beans:property name="prefixJson" value="true" />
</beans:bean>
</beans:list>
</beans:property>
<beans:property name="viewResolvers">
<beans:list>
<beans:bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:list>
</beans:property>
Any help would be greatly appreciated. I have blown through 6 hours on a sunday trying to figure out why this is happening.
From the
MappingJacksonJsonView
javadoc :So did you try with prefixJson set to false ?
I was receiving the same stuff and I've made the same mistake as you did in the code you provided :) I just accidentally forgot to add annotation
@ResponseBody
to the controller method.