1

I am using spring 3.0 in my webapplication. I've got recently a problem in my application. I am using <mvc:annotation-drive/> tag in my spring-servlet.xml file but due to a requirement I've to remove this and place XML configuration instead.

But now my problem is it generates json output with quoted field names like if I return Boolean.TRUE I got "true" in the output. I want just true without quotes.

here is my XML configuration

<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>

any help would be much appreciable.

4

4 回答 4

0

这是默认行为,如果您希望布尔值不带引号,请使用 JAXB ContextResolver

像这样的东西

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;

import com.sun.jersey.api.json.JSONJAXBContext;

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
     private JAXBContext context;
     private Class[] types = {SomeClass.class}; //Add the classes processed by JAXB and exposing boolean properties

     public JAXBContextResolver() throws Exception {
         Map props = new HashMap<String, Object>();
         props.put(JSONJAXBContext.JSON_NOTATION, JSONJAXBContext.JSONNotation.MAPPED);
         props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);

     java.util.HashSet<String> eprops = new HashSet<String>();
         eprops.add("someBooleanProperty"); //add properties you want unquoted
         props.put(JSONJAXBContext.JSON_NON_STRINGS, eprops);
         this.context = new JSONJAXBContext(types, props);
     }

     public JAXBContext getContext(Class<?> objectType) {
         return (types[0].equals(objectType)) ? context : null;
     }
}
于 2012-10-25T08:40:27.177 回答
0

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

于 2012-10-18T13:57:32.093 回答
0

如果你只返回原始值true(或者Boolean.TRUE.booleanValue()而不是包装的对象版本Boolean.TRUE?

于 2012-10-20T17:38:38.483 回答
0

我相信真值和假值在杰克逊库中是硬编码的(即使是装箱的布尔值也会调用 writeBoolean):

private final static byte[] TRUE_BYTES = { 't', 'r', 'u', 'e' };

@Override
public void writeBoolean(boolean state)
    throws IOException, JsonGenerationException
{
    _verifyValueWrite("write boolean value");
    if ((_outputTail + 5) >= _outputEnd) {
        _flushBuffer();
    }
    byte[] keyword = state ? TRUE_BYTES : FALSE_BYTES;
    int len = keyword.length;
    System.arraycopy(keyword, 0, _outputBuffer, _outputTail, len);
    _outputTail += len;
}

所以杰克逊永远不会在字段值中返回双引号的“true”(如果它的行为没有被严重覆盖,例如使用编解码器)。

所以请检查是否调用了MappingJacksonHttpMessageConverter方法,并且没有在其他地方执行转换。

于 2012-10-23T23:06:11.383 回答