2013-05-29 73 views
2

我正在使用JAXB将bean转换为JSON。当JAXB转换类型的bean值时,被截断。JAXB截断长整型值

例子:

如果我长的数值:44444444444444444
JAXB截断这样的:44444444444444450

谁能帮我解决这个问题呢?

感谢

回答

1

注:我是EclipseLink JAXB (MOXy)铅和JAXB (JSR-222)专家小组的成员。

JAXB(JSR-222)规范不包括将对象转换为JSON或从JSON转换对象。您遇到的问题来自于利用JAXB元数据的JSON绑定实现。下面我将演示当MOXy用作JSON绑定提供程序时,此用例可以很好地工作。

Java模型(美孚)

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class Foo { 

    private long bar; 

} 

jaxb.properties

要指定莫西为您的JAXB提供者,你需要包括在同一个包称为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 javax.xml.transform.stream.StreamSource; 

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[] {Foo.class}, properties); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     StreamSource json = new StreamSource("src/forum16821525/input.json"); 
     Foo foo = unmarshaller.unmarshal(json, Foo.class).getValue(); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(foo, System.out); 
    } 

} 

的input.xml /输出

{ 
    "bar" : 44444444444444444 
} 
+0

那好吧,我看不到与配置的联系,有工作的代理阻止我从访问。 我想创建一个拦截器来将long值转换为一个String? 我想这样的问题也解决了,对吗? –

+0

@ user2433783 - 你不需要那样做。你在运行什么环境?你用什么作为JSON绑定提供者? –

+0

我使用默认的libs服务器Jboss 7.1,项目的其中一个要求是使用默认值以外的任何lib。 –