如何使用JAXB将长属性作为JSON字符串值返回 [英] How to return a long property as JSON string value with JAXB
问题描述
我有一个用 @XmlRootElement
注释的Java类。这个Java类有一个long属性( private long id
),我希望它返回给JavaScript-client。
I have a Java class annotated with @XmlRootElement
. This Java class has a long property (private long id
) that I want to return to a JavaScript-client.
我按如下方式创建JSON:
I create the JSON as follows:
MyEntity myInstance = new MyEntity("Benny Neugebauer", 2517564202727464120);
StringWriter writer = new StringWriter();
JSONConfiguration config = JSONConfiguration.natural().build();
Class[] types = {MyEntity.class};
JSONJAXBContext context = new JSONJAXBContext(config, types);
JSONMarshaller marshaller = context.createJSONMarshaller();
marshaller.marshallToJSON(myInstance, writer);
json = writer.toString();
System.out.println(writer.toString());
这将生成:
{"name":"Benny Neugebauer","id":2517564202727464120}
但问题是JavaScript客户端的长值太大了。因此,我想将此值作为字符串返回(不要在Java中将long作为字符串)。
But the problem is that the long value is too large for the JavaScript client. Therefore, I would like to return this value as a string (without making the long a string in Java).
是否有可以生成的注释(或类似的东西)以下?
Is there an annotation (or something similar) that can generate the following?
{"name":"Benny Neugebauer","id":"2517564202727464120"}
推荐答案
注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
以下是如何使用MOXy作为JSON提供程序来完成此用例。
Below is how you could accomplish this use case with MOXy as your JSON provider.
MyEntity
您可以使用 @XmlSchemaType(name =string)<注释
表示它应该被编组为 long
属性字符串
。
package forum11737597;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class MyEntity {
private String name;
private long id;
public MyEntity() {
}
public MyEntity(String name, long id) {
setName(name);
setId(id);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlSchemaType(name="string")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
jaxb.properties
要将MOXy配置为JAXB提供程序,您需要在文件中包含一个名为 jaxb.properties
的文件。与您的域模型相同的包(请参阅: http:// blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html )。
To configure MOXy as your JAXB provider you need to include a file called jaxb.properties
in the same package as your domain model (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
我修改了示例代码,以显示使用MOXy时的样子。
I have modified your sample code to show what it would look like if you used MOXy.
package forum11737597;
import java.io.StringWriter;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
MyEntity myInstance = new MyEntity("Benny Neugebauer", 2517564202727464120L);
StringWriter writer = new StringWriter();
Map<String, Object> config = new HashMap<String, Object>(2);
config.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
config.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
Class[] types = {MyEntity.class};
JAXBContext context = JAXBContext.newInstance(types, config);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(myInstance, writer);
System.out.println(writer.toString());
}
}
输出
以下是运行演示代码的输出:
Below is the output from running the demo code:
{"id":"2517564202727464120","name":"Benny Neugebauer"}
更多信息
- http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
这篇关于如何使用JAXB将长属性作为JSON字符串值返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!