- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
将 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
更严格 Marshaller
和 Unmarhaller
捕捉这种类型的错误。它可以帮助您找到真正的原因。
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
}
}
}
替换你的 JaxbDataFormat
与 StrictJaxbDataFormat
final StrictJaxbDataFormat unmarshalFormat = new StrictJaxbDataFormat();
我已尝试模拟您的代码,它的行为与您描述的完全一样(id
和 title
为空)。当我添加 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/
我有以下 json: {"results": [{"columns":["room_id","player_name","player_ip"], "types":["integer","text
我在 go 中获取格式不一致的 JSON 文件。例如,我可以有以下内容: {"email": "\"blah.blah@blah.com\""} {"email": "robert@gmail.com
JavaScript中有JSON编码/解码base64编码/解码函数吗? 最佳答案 是的,btoa() 和 atob() 在某些浏览器中可以工作: var enc = btoa("this is so
我在其中一个项目中使用了 Encog,但在解码 One-Of Class 时卡住了。该字段的规范化操作之一是 NormalizationAction.OneOf,它具有三个输出。当我评估时,我想解码预
在我的 previous question关于使用 serialize() 创建对象的 CSV 我从 jmoy 那里得到了一个很好的答案,他推荐了我的序列化文本的 base64 编码。这正是我要找的。
有些事情让我感到困惑 - 为什么 this image在每个浏览器中显示不同? IE9(和 Windows 照片查看器)中的图像: Firefox(和 Photoshop)中的图像: Chrome(和
是否可以在不知道它的类型( JAXBContext.newInstance(clazz) )的情况下解码一个类,或者什么是测试即将到来的正确方法? 我确实收到了从纯文本中解码的消息 - 字符串 传入的
我正在尝试使用 openSSL 库进行 Base64 解码,然后使用 CMS 来验证签名。 下面的代码总是将缓冲区打印为 NULL。 char signed_data[] = "MIIO"; int
我有一个带有 SEL 类型实例变量的类,它是对选择器的引用。在encodeWithCoder/initWithCoder中,如何编码/解码这种类型的变量? 最佳答案 您可以使用 NSStringFro
var url = 'http://www.googleapis.com/customsearch/v1?q=foo&searchType=image'; window.fetch(url) .t
我想知道Android 2.2、2.3和3,4支持的音频/视频格式列表。我也想知道哪些Android版本支持视频编码和解码。我经历了this link,但是关于编码和解码我并不清楚。 任何人的回答都是
我在其中一个项目中使用 Encog,但在解码 One-Of 类时遇到了困难。该字段的规范化操作之一是 NormalizationAction.OneOf,它具有三个输出。当我评估时,我想解码预测值。如
我正在尝试解码现有的 xml 文件,以便我可以正确处理数据,但 XML 结构看起来很奇怪。下面是 xml 示例以及我创建的对象。 11 266 AA1001 1
对 unicode 字符进行 URL 编码的常用方法是将其拆分为 2 %HH 代码。 (\u4161 => %41%61) 但是,unicode在解码时是如何区分的呢?您如何知道 %41%61 是 \
我正在尝试将 json 字符串解码为 Map。 我知道有很多这样的问题,但我需要非常具体的格式。例如,我有 json 字符串: { "map": { "a": "b",
我有一个查询,我认为需要像这样(解码会更大) SELECT firstName, lastName, decode(mathMrk, 80, 'A', mathMrk) as decodeMat
我知道PHP函数encode()和decode(),它们对我来说工作得很好,但我想在url中传递编码字符串,但encode确实返回特殊字符,如“=”、“”' “等等...... 这显然会破坏我的脚本,
我必须解码 Basic bW9uTG9naW46bW9uTW90RGVQYXNz 形式的 http 请求的授权 header 当我解码它时online ,我得到了正确的结果 monLogin:monM
这个问题已经有答案了: Decode Base64 data in Java (21 个回答) 已关闭 8 年前。 我想知道使用哪个库进行 Base64 编码/解码?我需要此功能足够稳定以供生产使用。
我正在尝试从 Arduino BT 解码 []byte,我的连接完美,问题是当我尝试解码数组时。我得到的只是这个字符�(发送的字节数相同)我认为问题出在解码上。我尝试使用 ASCII 字符集,但仍然存
我是一名优秀的程序员,十分优秀!