当我使用此属性封送XML时
marshal.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)
它会在最顶部产生一个空的换行符
//Generate empty line break here
<XX>
<YY>
<PDF>pdf name</PDF>
<ZIP>zip name</ZIP>
<RECEIVED_DT>received date time</RECEIVED_DT>
</YY>
</XX>
Run Code Online (Sandbox Code Playgroud)
我认为原因是因为marshal.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);删除<?xml version="1.0" encoding="UTF-8" standalone="yes"?>了输出xml在开头的一个换行符.有没有办法来解决这个问题?我使用JAXB自带JDK 6,Moxy会遇到这个问题吗?
我有一个示例课程:
class Zoo {
public Collection<? extends Animal> animals;
}
Run Code Online (Sandbox Code Playgroud)
当使用MOXy序列化时,我得到:
{
"bird": [
{
"name": "bird-1",
"wingSpan": "6 feets",
"preferredFood": "food-1"
}
],
"cat": [
{
"name": "cat-1",
"favoriteToy": "toy-1"
}
],
"dog": [
{
"name": "dog-1",
"breed": "bread-1",
"leashColor": "black"
}
]
}
Run Code Online (Sandbox Code Playgroud)
为什么它使用数组指示符"[]",而鸟,猫和狗不是数组?第二,有没有办法摆脱"鸟","猫"和"狗"?
换句话说,我试图去:
{
{
"name": "bird-1",
"wingSpan": "6 feets",
"preferredFood": "food-1"
}
,
{
"name": "cat-1",
"favoriteToy": "toy-1"
}
,
{
"name": "dog-1",
"breed": "bread-1",
"leashColor": "black"
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢,Behzad
我有我的模特:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomerTest {
private Long id;
@XmlPath("contact-info/billing-address")
private AddressTest billingAddress;
@XmlPath("contact-info/shipping-address")
private AddressTest shippingAddress;
@XmlPath("FileHeader/SchemaVersion/text()")
private String schemaVersion;
}
Run Code Online (Sandbox Code Playgroud)
我填写这样的对象:
private void marshallCustomerTest() {
try {
JAXBContext jc = JAXBContext.newInstance(CustomerTest.class);
CustomerTest customer = new CustomerTest();
customer.setId(new Long(10));
customer.setSchemaVersion("3.2");
AddressTest billingAddress = new AddressTest();
billingAddress.setStreet("1 Billing Street");
customer.setBillingAddress(billingAddress);
AddressTest shippingAddress = new AddressTest();
shippingAddress.setStreet("2 Shipping Road");
customer.setShippingAddress(shippingAddress);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(customer, System.out);
} catch (JAXBException jex) {
jex.printStackTrace();
log.error(jex);
}
}
Run Code Online (Sandbox Code Playgroud)
这产生了下一个XML:
<customerTest xmlns:fe="http://www.facturae.es/Facturae/2009/v3.2/Facturae" …Run Code Online (Sandbox Code Playgroud) 如何type从我拥有的JSON输出中删除.我有一个包含REST服务输出的类/ bean.我正在使用jersey-media-moxy转换.
服务
@Resource
public interface MyBeanResource
{
@GET
@Path("/example")
@Produces( MediaType.APPLICATION_JSON )
public Bean getBean();
}
Run Code Online (Sandbox Code Playgroud)
豆
@XmlRootElement
class Bean
{
String a;
}
Run Code Online (Sandbox Code Playgroud)
我想添加一些功能(用于使用构造函数初始化bean)
class BeanImpl extends Bean
{
BeanImpl(OtherClass c)
{
a = c.toString()
}
}
Run Code Online (Sandbox Code Playgroud)
输出的JSON是:
{type:"beanImpl", a:"somevalue"}
我不想要type我的JSON.我该如何配置?
我有XSD文件形式的数据模型,然后从中xjc使用命令行生成相应的Java文件.
当我从XSD生成JAXB类时,List类型元素获取为它们生成的getter方法(没有相应的setter方法),如下所示:
public List<Type> getElement3() {
if (element3 == null) {
element3 = new ArrayList<Type>();
}
return this.element3;
}
Run Code Online (Sandbox Code Playgroud)
我在几乎所有从XSD List类型生成的文件中都有很多字段.
使用案例:
现在,我不希望通过null检查生成getter .我的应用程序代码具有逻辑,其中每个字段的getter经常被调用,这导致它们的初始化为空 List.
然后在编组时我必须停止空列表以传递有效负载,以避免通过线路发送大量空列表.
PS:我有一个用例明确Empty List设置的用例,服务器必须删除后端的某些项目.因此,区分值是由用户显式设置还是仅因为在访问字段期间调用了getter而设置的.List
那么,如何让JAXB生成一个没有null检查的getter ?
因为在编译之后编辑生成的java文件会很麻烦,因为它存在于很多文件中,我们有XSD版本需要更新,每次新版本出现时都必须执行编辑.
为了使用Moxy作为JAXB实现,必须在包含应用程序的域类的文件夹中添加一个名为jaxb.properties的文件,并包含以下行
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Run Code Online (Sandbox Code Playgroud)
是否可以将moxy作为项目的默认值?如果我有许多不同的带有注释域类的文件夹,我应该在哪个文件夹中放置这个文件?
我有一个Map<String, String>.
每个人的第一个想法是将其转换为List<Pair<String,String>>(Pair作为自定义类).
我试过@XmlAdapter这样的:
public class MapPropertiesAdapter extends XmlAdapter<List<Property>, Map<String,String>> { ... }
Run Code Online (Sandbox Code Playgroud)
但Eclipse MOXy,我使用的JAXB impl,最终得到了ClassCastException- "无法将HashMap转换为Collection".
JAXB是否支持此转换?或者我忽略了一些文档部分,它解释了为什么不是?
PS:我想得到这样的XML:
<properties>
<property name="protocol"/>
<property name="marshaller"/>
<property name="unmarshaller"/>
<property name="timeout"/>
...
</properties>
Run Code Online (Sandbox Code Playgroud)
我明白了,只需要使用中级课程.还在XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:161)中的Handle NPE中进行了描述
我正在浏览Blaise的博客http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html,了解使用Substitution的Jaxb继承.
我想实现相同但不是根元素.我正在寻找这种类型的XML作为输出.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
<customer>
<address>
<street>1 A Street</street>
</address>
<address>
<street>2 B Street</street>
</address>
<phoneNumber>
<mobileNo>xxx-xxx-xxxx</mobileNo>
</phoneNumber>
</customer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
以下是Configuration.java
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Configuration {
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "\n Customer[ customer="+customer+"]";
}
}
Run Code Online (Sandbox Code Playgroud)
Customer.java
public class Customer {
private List<ContactInfo> contactInfo;
@XmlElementRef
public List<ContactInfo> getContactInfo() {
return contactInfo;
}
public …Run Code Online (Sandbox Code Playgroud) 我正在使用MOXy和Jersey实现一个RESTful API,并希望自然地为JSON和XML返回列表,我的意思是XML包含整个集合的元素标记以及集合项,而JSON包含仅用于集合的标记.
例如,我想返回一个"组织"资源,其中包含设施和位置的嵌套列表.作为XML:
<organisation>
<id>1</id>
<name>XYZ</name>
<facilities>
<facility>
<id>1</id>
<text>Telephone</text>
</facility>
<facility>
<id>3</id>
<text>Whiteboard</text>
</facility>
</facilities>
<locations>
<location>
<id>1</id>
<kind>REGION</kind>
<name>London</name>
</location>
<location>
<id>2</id>
<kind>REGION</kind>
<name>Manchester</name>
</location>
</locations>
</organisation>
Run Code Online (Sandbox Code Playgroud)
而作为JSON:
{
"id": 1,
"name": "XYZ",
"facilities": [
{
"id": 1,
"text": "Telephone"
},
{
"id": 3,
"text": "Whiteboard"
}
],
"locations": [
{
"id": 1,
"kind": "REGION",
"name": "London"
},
{
"id": 2,
"kind": "REGION",
"name": "Manchester"
}
]
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我似乎无法获得允许我为XML和JSON返回这样的输出的代码库.如果我使用一个类来包装嵌套列表,那么XML看起来是正确的但不是JSON(参见下面的"ExternalFacilities").如果我将嵌套列表定义为ArrayList子类,则JSON显示正确但不是XML(请参阅下面的"ExternalLocations").
XML示例显示"设施"列表正确但不是"位置"
看到没有包含"位置"列表的XML元素(就像"设施"一样),并且每个位置都有一个复数元素名称.
<organisation>
<id>1</id>
<name>XYZ</name>
<facilities>
<facility> …Run Code Online (Sandbox Code Playgroud) moxy ×10
java ×8
jaxb ×7
json ×4
eclipselink ×3
marshalling ×3
xml ×3
jersey ×1
xjc ×1
xmladapter ×1