【问题标题】:Jettison with json marshalling converting string data type to integer type, when value is numeric带有json编组的Jettison将字符串数据类型转换为整数类型,当值为数字时
【发布时间】:2013-05-03 20:13:09
【问题描述】:

我们正在使用 jettison-1.3.3 将 JaxB 转换为 Json。

我们正面临一个问题。 每当我有一个包含所有数字的 String 属性(即 String phone="12345";)时,JSON 响应都会将其显示为数字 (12345),而无需双引号。

如果在这种情况下该值为 1234AS,则返回双引号。 如何解决这个问题并确保它总是有双引号。

请帮忙

【问题讨论】:

    标签: java json rest jakarta-ee jettison


    【解决方案1】:

    抛弃中有类型转换器。默认情况下,它使用默认类型转换器。 如果值是数字类型,默认类型转换会删除双引号。

    要始终使用双引号,请使用 SimpleConverter。

    创建系统属性 - IE System.setProperty("jettison.mapped.typeconverter.class", "org.codehaus.jettison.mapped.SimpleConverter");

    所以 jettison 使用简单的转换器并将值作为字符串输出。

    【讨论】:

    • 嗨,Narendra Reddy,感谢您的重播,我使用 jackson 而不是 jettison 并找到了解决方案。
    【解决方案2】:

    注意:我是EclipseLink JAXB (MOXy) 的负责人,也是JAXB (JSR-222) 专家组的成员。

    对于这个用例,您可以使用 MOXy 提供的 JSON 绑定。

    域模型(根)

    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Root {
    
        int number;
        String string;
    
    }
    

    将 MOXy 指定为 JSON 绑定提供程序

    在 RESTful 环境中,您可以将 MOXyJsonProvider 指定为您的 JAX-RS 应用程序的 MessageBodyReader/MessageBodyWriter

    在下面的独立示例中,您可以使用以下条目在与域模型相同的包中指定 jaxb.properties 文件(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    演示代码

    下面是一个独立的示例,您可以运行它来证明一切正常:

    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(2);
            properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);
    
            Root root = new Root();
            root.number = 1234;
            root.string = "1234";
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    输出

    下面是运行演示代码的输出:

    {
       "number" : 1234,
       "string" : "1234"
    }
    

    【讨论】:

    • 感谢您的回复。我无法下载用于导入 org.eclipse.persistence.jaxb.JAXBContextProperties 和导入 org.eclipse.persistence.jaxb.MarshallerProperties 的 jar 文件,如果我知道从哪里可以获得链接,我必须使用哪个 jar 会很有帮助它。
    • @user2365956 - 你可以在这里下载 EclipseLink:eclipse.org/eclipselink/downloads
    • 感谢您的快速响应我面临 javax.xml.bind.JAXBException: property "eclipselink.media-type" is not supported exception
    • +已添加,使用 jboss 版本 5.1.0.GA
    【解决方案3】:

    这似乎是 Jettison 的一个隐含“特征”;它试图反省实际数据并找出最适合的类型。最好尝试使用其他一些库,例如 Jackson。 Jackson 不会尝试提供一些导致此类问题的不必要的帮助。

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2019-02-09
      相关资源
      最近更新 更多