eclipselink
首先是一个简单的测试,将Java对象编组为XML:
这是模型:
@XmlRootElement(name='MemberDetailsRequest', namespace='http://bk.org/memberservice/')
@XmlAccessorType(XmlAccessType.FIELD)
public class MemberDetailsRequest {
public MemberDetailsRequest() {
}
public MemberDetailsRequest(Long id) {
this.id = id;
}
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
和封送处理的测试:
package org.bk.memberservice.binding;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import org.bk.memberservice.message.MemberDetailsRequest;
import org.junit.Test;
public class TestJaxbRequestBinding {
@Test
public void testJaxbBinding() throws Exception{
JAXBContext jaxbContext = JAXBContext.newInstance(MemberDetailsRequest.class);
assertThat((jaxbContext instanceof org.eclipse.persistence.jaxb.JAXBContext), is(true));
Marshaller marshaller = jaxbContext.createMarshaller();
MemberDetailsRequest memberDetailsRequest = new MemberDetailsRequest();
memberDetailsRequest.setId(1L);
StringWriter writer = new StringWriter();
marshaller.marshal(memberDetailsRequest, writer);
String marshalledXml = writer.toString();
assertThat(marshalledXml, containsString('MemberDetailsRequest'));
}
}
高亮显示的行进行检查以确保MOXy的JAXBContext是创建的那个。
因此,要将MOXy用作JAXB提供程序, 第一步是获取jar文件,
我很难找到要在Maven pom文件中使用的MOXy罐子,这对我有用:
<repository>
<id>EclipseLink Repo</id>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
<name>EclipseLink Repo</name>
</repository>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.4.0</version>
</dependency>
下一步是指示将MOXy设置为JAXB提供程序。 这是通过将jaxb.properties与任何JAXB模型类一起放置来完成的。 因此,在这种情况下,我将jaxb.properties放入包含MemberDetailsRequest类的包中,其内容如下:
javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory
就是这样!
参考: all和其他博客中的EclipseLink MOXy作为我们的JCG合作伙伴 Biju Kunjummen 的JAXB提供者 。
翻译自: https://www.javacodegeeks.com/2012/07/eclipselink-moxy-as-jaxb-provider.html
eclipselink