从控制器返回后JSON被编码

时间:2013-12-24 04:08:55

标签: json spring-mvc servlets spring-3

Spring控制器具有以下方法:

@RequestMapping(value = "reportsOverall.html")
    public/* @ResponseBody */
    ModelAndView reportsOverall(SearchParameters searchParameters) {
        searchParameters = new SearchParameters(840, "SCH_01", "PRM_07");
        System.out.println("Inside controller");
        String result = overallDetailsService
                .getOverallDetails(searchParameters);

        System.out.println("result is " + result);

        Map<String, String> model = new HashMap<String, String>();
        model.put("data", result);

        ModelAndView modelAndView = new ModelAndView("reports/reportsOverall",
                model);

        return modelAndView;

    }

我在服务器端控制台上获得了预期的输出:

result is { "_id" : { "SpId" : 840 , "Scheduler_id" : "SCH_01"} , "positive_count" : 64 , "neutral_count" : 78 , "negative_count" : 2}

但是在reportsOverall.jsp中,当我将结果对象检索为:

function drawSentimentPieChart() {

        var data = '<c:out value="${data}"/>';
        alert(data);
            var dataJson = JSON.parse(data);

......
}

在警报中我得到了:

{ &#034;_id&#034; : { &#034;SpId&#034; : 840 , &#034;Scheduler_id&#034; : &#034;SCH_01&#034;} , &#034;positive_count&#034; : 64 , &#034;neutral_count&#034; : 78 , &#034;negative_count&#034; : 2}

此处对空格和“”引号进行编码,从而导致解析失败。

spring-servlet.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <mvc:annotation-driven />
    <context:component-scan base-package="com.lnt" />

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"></bean>

    <!-- <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <array>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
                </bean>
            </array>
        </property>
    </bean> -->

    <bean id="jacksonMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

        <property name="basenames"
            value="classpath:userProfile,classpath:default,classpath:messages,classpath:customerAdmin,classpath:reports,classpath:superAdmin" />

        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="dateFormat" class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd hh:mm:ss" />
    </bean>


</beans>

我在Spring控制器中尝试了几件事:

使用@ResponseBody;在这种情况下,我在页面上有一个像这样的转储,但页面没有正确呈现:

{"view":null,"model":{"data":"{ \"_id\" : { \"SpId\" : 840 , \"Scheduler_id\" : \"SCH_01\"} , \"positive_count\" : 64 , \"neutral_count\" : 78 , \"negative_count\" : 2}"},"empty":false,"viewName":"reports/reportsOverall","reference":true,"modelMap":{"data":"{ \"_id\" : { \"SpId\" : 840 , \"Scheduler_id\" : \"SCH_01\"} , \"positive_count\" : 64 , \"neutral_count\" : 78 , \"negative_count\" : 2}"}}

在web.xml中添加了编码过滤器:

<filter>
        <filter-name>encoding-filter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>

    </filter>

    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

徒劳地尝试使用MappingJacksonJsonView!

我在俯瞰什么?

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,但这个额外的属性救了我:

<c:out value="${data}" escapeXml="false"/>