有没有办法在 JAXB 中自定义 XML 序列化,就像在 .NET 中使用 IXmlSerializable 一样?(即使用等效的 XmlReader/Writer 直接控制对象序列化的能力)。
我查看了 XmlAdapter 和 @XmlJavaTypeAdapter,但它们似乎只是用于在类型与可序列化形式之间进行转换,这并不是我想要的。
更新:特别是,我想自定义根对象的反序列化,以编程方式确定如何反序列化内部 XML(例如,使用一组特定的已知类型创建一个 jaxb unmarshaller)。
更新:我找到了解决问题的方法,但这是一个令人讨厌的黑客攻击,我可能会采用其他海报建议的解决方案之一。
我有一个响应 xml 数据的 Web 服务器和一个使用它的客户端。两者共享相同的域代码。域对象之一如下所示:
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement(name = "image")
public class Image {
private String filename;
private ImageTypeEnum type;
@XmlElement(name = "imageUri")
public String getAbsoluteUri() {
// some complex computation
return uri;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将来自服务器的响应解组到这个对象中时,由于没有绝对 Uri 的设置器,我在类中没有 imageUri。所以我像这样扩展它:
public class FEImage extends Image{
private String imageUri;
public String getAbsoluteUri() {
return imageUri;
}
public void setAbsoluteUri(String imageUri) {
this.imageUri = imageUri;
}
}
Run Code Online (Sandbox Code Playgroud)
我的对象工厂
@XmlRegistry
public class ObjectFactory {
public Image createImage(){
return new FEImage();
}
}
Run Code Online (Sandbox Code Playgroud)
我的解组代码在这里:
JAXBContext context …Run Code Online (Sandbox Code Playgroud) 我有以下 JAXB 注释类(用 XJC 生成)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ReplicateTransactionDataNotificationType", namespace = "http://esb.pkobp/adapter/calypso/tradekast/transaction", propOrder = {
"operationContext",
"transactionData"
})
public class ReplicateTransactionDataNotificationType
implements Serializable
{
@XmlElement(name = "OperationContext", required = true)
protected OperationContextType operationContext;
@XmlElement(name = "TransactionData")
protected TransactionDataType transactionData;
public OperationContextType getOperationContext() {
return operationContext;
}
public void setOperationContext(OperationContextType value) {
this.operationContext = value;
}
public TransactionDataType getTransactionData() {
return transactionData;
}
public void setTransactionData(TransactionDataType value) {
this.transactionData = value;
}
}
Run Code Online (Sandbox Code Playgroud)
按以下方式解组时,一切正常:
JAXBContext ctx = JAXBContext.newInstance(ReplicateTransactionDataNotificationType.class);
Unmarshaller unmarshaller = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 maven cxf-codegen-plugin 从 WSDL 创建一个 Java 类,但是当我尝试这样做时,它只显示一个错误并且没有生成类。
我不明白为什么我有这个错误,我的 pom 文件看起来不错。
我的pom.xml样子
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-hc</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration> …Run Code Online (Sandbox Code Playgroud) 我这里有一个情况,试图充当两个 API 之间的网关。我需要做的是:
问题是我使用相同的对象来解析 API 响应并将响应发送到另一端。
public class ResponseAPI{
@XmlElement(name="ResponseCode") //I receive <ResponseCode> but I need to send <ResultCode>
private String responseCode;
//getter and setter
}
Run Code Online (Sandbox Code Playgroud)
正如评论所说:我收到但我需要发送
有没有办法在不必创建另一个带有 ResultCode 的额外类的情况下完成这项工作?
提前致谢!
我是 Apache camel 的新手,需要执行一项需要将对象编组到 XML 文件的任务。我正在使用下面的代码,但它不起作用。这里foo.pojo是存在 JAXB 注释类的包
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat("foo.pojo");
from("direct:start").marshal(jaxbDataFormat).to("file:C:/Users/Anand.Jain/Desktop/hello/abc.xml").end();
Run Code Online (Sandbox Code Playgroud) 旧站点上有一些文档, 但不清楚在使用绑定文件时如何应用配置。
这是我用来向生成的 JAXB 对象添加 toString、equals 等的 Maven 配置。
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaIncludes>
<include>*.xsd</include>
</schemaIncludes>
<args>
<arg>-Xfluent-api</arg>
<arg>-XautoNameResolution</arg>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xcopyable</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-fluent-api</artifactId>
<version>3.0</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.9.4</version>
</plugin>
</plugins>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud) 我知道 Hyperjaxb3 库对我的项目非常有用,在多个站点上阅读了一些描述,并决定将它嵌入到我的 Spring-Hibernate 项目中。
我在https://jaxb.java.net/ 中找到了对 Hyperjaxb3 的引用,看起来很官方,但是超链接 - http://confluence.highsource.org/display/HJ3/Home - 没有打开。
我找到了一些旧的 POM 示例,将其包含在我的项目中,并找到了一些旧版本的引用,试图消除它们,但现在似乎我遇到了对旧 Hibernate 版本的依赖,错误就像这个:
java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: Provider org.jvnet.hyperjaxb3.hibernate.plugin.HibernatePlugin 无法实例化: java.lang.NoClassDefFoundError: org/hibernate/type/MutableType
我想知道是否有更好的 Maven 条目,项目是否还活着以及如何将它与现代 Hibernate 一起使用。
这是我关于 Hyperjaxb3 的 pom 摘录,其中我排除了一些过时的链接并指定了其他依赖项的最新版本:
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-core</artifactId>
<version>${jaxb-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>${jaxb-version}</version>
</dependency>
<!--<dependency>
<groupId>org.jvnet.hyperjaxb3</groupId>
<artifactId>hyperjaxb3</artifactId>
<version>0.6.1</version>
</dependency> -->
<dependency>
<groupId>org.jvnet.hyperjaxb3</groupId>
<artifactId>hyperjaxb3-hibernate-plugin</artifactId>
<version>0.1</version>
<exclusions>
<exclusion>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>net.sf.saxon</groupId>
<artifactId>saxon</artifactId>
</exclusion>
<exclusion>
<groupId>net.sf.saxon</groupId>
<artifactId>saxon-dom</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId> …Run Code Online (Sandbox Code Playgroud) 因此,我可以使用 ant xjc 从合同 jar 中的 ONE xsd 生成类。如何在不解压的情况下从这个 jar 的多个模式生成类
ant.xjc(package: packageName, destdir: project.ext.generatedSrcDir,
extension: 'true',
schema: "jar:file:///$pathToContractJar!/MySchema.xsd")
Run Code Online (Sandbox Code Playgroud) 我正在尝试针对XML文件验证JAXB模式,并且不断收到上述错误。您能告诉我<{} Quote>是什么意思吗?错误是从哪里来的?报价是从XSD文件创建的。是否因为未定义“引号”元素?这是我的代码。
package khoa;
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
import khoa.Quote;
import khoa.Quotes;
public class EmployeeUnmarshaller {
public static void main(String[] args) throws JAXBException, SAXException {
new EmployeeUnmarshaller().runEmployeeUnmarshaller();
}
private void runEmployeeUnmarshaller() throws JAXBException, SAXException {
JAXBContext context = JAXBContext.newInstance(Quote.class);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("quotes.xsd"));
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(new EmployeeValidationEventHandler());
System.out.println(unmarshaller.unmarshal(new File("quotes.xml")));
}
}
class EmployeeValidationEventHandler implements …Run Code Online (Sandbox Code Playgroud) jaxb ×10
java ×5
xml ×3
xjc ×2
apache-camel ×1
cxf ×1
gradle ×1
hyperjaxb ×1
jaxb2-basics ×1
marshalling ×1
maven ×1
wsdl2java ×1
xml-parsing ×1
xsd ×1