I am getting an extra empty {} json object along with a && before my output from a simple spring mvc json service

坚强是说给别人听的谎言 提交于 2020-01-21 12:14:11

问题


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.


回答1:


From the MappingJacksonJsonView javadoc :

public void setPrefixJson(boolean prefixJson)

Indicates whether the JSON output by this view should be prefixed with "{} && ". Default is false. Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. This prefix does not affect the evaluation of JSON, but if JSON validation is performed on the string, the prefix would need to be ignored.

So did you try with prefixJson set to false ?

<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="false" />
            </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>



回答2:


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.



来源:https://stackoverflow.com/questions/14188545/i-am-getting-an-extra-empty-json-object-along-with-a-before-my-output-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!