gpt4 book ai didi

java - 从 Camel 从 2.20.4 升级到 2.21.2 或 2.22.1 后,Apache Camel JAXB 解码返回空属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:16 25 4
gpt4 key购买 nike

将 Apache Camel 从 2.20.4 升级到 2.21.2 甚至 2.22.1 后,我的单元测试失败了,我不明白为什么。

我有一个接收 XML 响应的路由,我想将其解码到一个数据类中。自 Apache Camel 2.21.2 以来,这失败了。

JAXB 上下文知道给定包中的所有数据类并选择正确的类。创建了正确的对象本身,但属性保持为空。通过调试 SAX 解析器,似乎标签之间的文本被 SAX 解析器故意忽略了。

我在 Camel 网站上看到 Camel 2.21.0“升级到 JAXB 2.3.0”。我不知道这对我意味着什么。

我的路线是这样的:

@Component
public class NewsletterRoute extends RouteBuilder {

8<-------------------------------------------------

@Override
public void configure() {
final DataFormat marshalFormat =
new JacksonDataFormat(HybrisRequest.class);
final JaxbDataFormat unmarshalFormat = new JaxbDataFormat();
unmarshalFormat.setContextPath(
SuccessResponse.class.getPackage().getName());

from(URI)
.routeId("subscribeRoute")
// Fetch the auth token to send it as 'x-csrf-token' header.
.setHeader(Exchange.HTTP_URI,
method(this.backendUrlProvider, "getUrl"))
.setHeader(Exchange.HTTP_PATH,
constant(this.subscribeUnsubscribePath))
.setHeader(Exchange.HTTP_METHOD, HttpMethods.POST)
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
// Marshal body to JSON
.marshal(marshalFormat)
.to(String.format("https4:backend" +
"?sslContextParameters=hybrisSslContextParams" +
"&x509HostnameVerifier=hybrisHostnameVerifier" +
// don't throw on 3XX/4XX/5XX since
// we need the response body
"&throwExceptionOnFailure=false" +
"&cookieStore=#hybrisCookieStore" +
"&authUsername=%s" +
"&authPassword=%s" +
"&authenticationPreemptive=true" +
"&httpClient.redirectsEnabled=true" +
"&httpClient.maxRedirects=3" +
"&httpClient.connectTimeout=%d" +
"&httpClient.socketTimeout=%d",
"RAW(" + username + ")",
"RAW(" + password + ")",
backendTimeoutMillis,
backendTimeoutMillis))
.convertBodyTo(String.class)
// Store the received response as property before
// trying to unmarshal it
.setProperty(PROPERTY_RESPONSE_RAW, body())
.unmarshal(unmarshalFormat);
// @formatter:on
}
}

数据类看起来像这样

@XmlRootElement(name = "entry", namespace = "http://www.w3.org/2005/Atom")
public class SuccessResponse {
private String id;
private String title;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String toString() { /*code cut out*/}
}

使用 Apache Camel 2.20.4 我的单元测试成功了。对于 2.21.2 和 2.22.1,它会中断。问题是,解码器不会填充属性。

单元测试只是向返回 XML 的 Mockito 发送一个有效请求。

<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="https://xxx.xxx.xxx.xxx:44380/sap/opu/odata/sap/CUAN_IMPORT_SRV/"
xmlns="http://www.w3.org/2005/Atom"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<!-- 8<---------- Code cut out here ----------------------------------- -->
<id>bla</id>
<title type="text">blub</title>
</entry>

有什么想法吗? camel-jaxb 中的错误?

最佳答案

我已经创建了自定义 DataFormat更严格 MarshallerUnmarhaller捕捉这种类型的错误。它可以帮助您找到真正的原因。

import org.apache.camel.converter.jaxb.JaxbDataFormat;

import javax.xml.bind.*;

public class StrictJaxbDataFormat extends JaxbDataFormat {

@Override
protected Unmarshaller createUnmarshaller() throws JAXBException {
Unmarshaller unmarshaller = super.createUnmarshaller();
unmarshaller.setEventHandler(new StrictValidationEventHandler());
return unmarshaller;
}

@Override
protected Marshaller createMarshaller() throws JAXBException {
Marshaller marshaller = super.createMarshaller();
marshaller.setEventHandler(new StrictValidationEventHandler());
return marshaller;
}

private static class StrictValidationEventHandler implements ValidationEventHandler {
@Override
public boolean handleEvent(ValidationEvent event) {
return false; // all validation events should throw exception
}
}
}

替换你的 JaxbDataFormatStrictJaxbDataFormat

final StrictJaxbDataFormat unmarshalFormat = new StrictJaxbDataFormat();

我已尝试模拟您的代码,它的行为与您描述的完全一样(idtitle 为空)。当我添加 StrictJaxbDataFormat , 它抛出 javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2005/Atom", local:"id"). Expected elements are <{}id>,<{}title>所以可能是这个问题JAXB unmarshalling error: Expected elements are <{ } Root>你应该添加 package-info.java使用 JAXB 类添加到您的包中。

@XmlSchema(namespace = "http://www.w3.org/2005/Atom", elementFormDefault = XmlNsForm.QUALIFIED)
package your.package;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

关于java - 从 Camel 从 2.20.4 升级到 2.21.2 或 2.22.1 后,Apache Camel JAXB 解码返回空属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52716828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com