java - Spring MVC 生成带引号的字段名称 json 输出

标签 java spring-mvc

我在我的网络应用程序中使用 spring 3.0。我最近在应用程序中遇到了问题。我正在使用<mvc:annotation-drive/>标记在我的 spring-servlet.xml 文件中,但由于要求,我必须删除它并放置 XML 配置。

但现在我的问题是它会生成带引号字段名称的 json 输出,就像我返回 Boolean.TRUE 一样。我得到了"true"在输出中。我想要的只是真实的,不带引号。

这是我的 XML 配置

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
    <bean id="pathMatcher" class="net.hrms.web.filters.CaseInsensitivePathMatcher" />

<bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="conversionService" ref="conversionService"></property>
        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <ref bean="byteArrayConverter"/>
            <ref bean="jaxbConverter"/>
            <ref bean="jsonHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
        </list>
    </property>
</bean>
<bean name="byteArrayConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean name="jaxbConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="pathMatcher" ref="pathMatcher"></property>
</bean>

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

  <bean id="jsonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="prefixJson" value="false"/>
    <property name="supportedMediaTypes" value="application/json"/>
  </bean>

任何帮助都会非常感激。

最佳答案

如果您使用 FlexJSON插件,您可以为 JSON 创建自定义输出。我相信你在 jackson 也能做到这一点,但我从来没有这样做过。 FlexJSON 网站上有大量示例。

关于java - Spring MVC 生成带引号的字段名称 json 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872824/

相关文章:

java - 使用守护进程(jsvc)将资源文件添加到类路径

java - 避免重复值

java - 如何从 Apache Beam 作业正确写入 InfluxDB?

java - 是否可以利用 ArrayList 作为 session 范围组件 - ModelAttribute - 而不是创建 pojo?

java - mysql jdbc 驱动程序类是否知道批量执行多个插入?

java - gridbaglayout 将组件定位到中心

css - 如何在 Spring MVC 中禁用标准 css

java - Spring Boot 中的 JAX-RS 和 MVC

java - Jasypt加密-加密时删除斜杠

java - Webjars 定位器不适用于基于 XML 的 Spring MVC 4.2.x 配置?