I am getting an extra empty {} json object along w

2020-04-10 01:07发布

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.

2条回答
虎瘦雄心在
2楼-- · 2020-04-10 01:38

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>
查看更多
Anthone
3楼-- · 2020-04-10 02:02

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.

查看更多
登录 后发表回答