gpt4 book ai didi

java - Citrus Simulator 在接收序列化 xml 数据时抛出空指针异常

转载 作者:行者123 更新时间:2023-12-02 00:29:00 26 4
gpt4 key购买 nike

我正在将 Citrus Simulator 作为 spring-boot 应用程序运行,以模拟响应 HTTP POST 调用的 Web 服务。在“实验室”条件下一切正常。只要我通过 Postman、curl 或浏览器插件手动发送数据,我就没有任何问题和干净的答案。然而,一旦数据被另一个应用程序发送,我就会得到一个令人讨厌的空指针异常。

我花了一段时间才查明问题所在,但现在我知道问题出在发送数据的格式上。

发送 pretty-print xml 数据会得到完美的答案。但是,一旦我序列化 xml,Citrus Simulator 将无法正确处理它,并且我将收到空指针异常

使用curl,我还可以通过简单地终止发送的xml 文件的格式来引发错误。

编辑 不是直接的格式。当以 pretty-print 格式发送数据时,这两个应用程序都会生成一个带有一些(奇怪的)内容的 HTTP header 元数据 ->curl 发送 Metadata="\t\t\t",postman 发送 Metadata="\n "。发送序列化的 xml,两个应用程序都发送空的元数据 header ,从而导致空指针异常就在这里 (MessageService.java:60) -> StringUtils.abbreviate(headerEntry.getValue().toString(), 255))));

java.lang.NullPointerException: null
at com.consol.citrus.simulator.service.MessageService.lambda$saveMessage$0(MessageService.java:60) ~[citrus-simulator-starter-1.0.3.jar:na]
at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_201]
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_201]
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) ~[na:1.8.0_201]
at com.consol.citrus.simulator.service.MessageService.saveMessage(MessageService.java:58) ~[citrus-simulator-starter-1.0.3.jar:na]
at com.consol.citrus.simulator.service.MessageService$$FastClassBySpringCGLIB$$be0e8eaa.invoke(<generated>) ~[citrus-simulator-starter-1.0.3.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736) ~[spring-aop-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671) ~[spring-aop-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at com.consol.citrus.simulator.service.MessageService$$EnhancerBySpringCGLIB$$f26ac16e.saveMessage(<generated>) ~[citrus-simulator-starter-1.0.3.jar:na]
at com.consol.citrus.simulator.service.ActivityService.saveScenarioMessage(ActivityService.java:124) ~[citrus-simulator-starter-1.0.3.jar:na]
at com.consol.citrus.simulator.service.ActivityService$$FastClassBySpringCGLIB$$1784762a.invoke(<generated>) ~[citrus-simulator-starter-1.0.3.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736) ~[spring-aop-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671) ~[spring-aop-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at com.consol.citrus.simulator.service.ActivityService$$EnhancerBySpringCGLIB$$35f927ee.saveScenarioMessage(<generated>) ~[citrus-simulator-starter-1.0.3.jar:na]
at com.consol.citrus.simulator.endpoint.EndpointMessageHandler.saveScenarioMessage(EndpointMessageHandler.java:58) ~[citrus-simulator-starter-1.0.3.jar:na]
at com.consol.citrus.simulator.endpoint.EndpointMessageHandler.handleReceivedMessage(EndpointMessageHandler.java:51) ~[citrus-simulator-starter-1.0.3.jar:na]
at com.consol.citrus.simulator.scenario.ScenarioEndpoint.messageReceived(ScenarioEndpoint.java:111) ~[citrus-simulator-starter-1.0.3.jar:na]
at com.consol.citrus.simulator.scenario.ScenarioEndpoint.receive(ScenarioEndpoint.java:88) ~[citrus-simulator-starter-1.0.3.jar:na]
at com.consol.citrus.simulator.scenario.SVManagerScenario.receive(SVManagerScenario.java:60) ~[classes/:na]
at com.consol.citrus.simulator.scenario.SVManagerScenario.run(SVManagerScenario.java:24) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:215) ~[spring-core-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at com.consol.citrus.simulator.service.ScenarioExecutionService.lambda$null$3(ScenarioExecutionService.java:150) ~[citrus-simulator-starter-1.0.3.jar:na]
at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:491) ~[spring-core-4.3.21.RELEASE.jar:4.3.21.RELEASE]
at com.consol.citrus.simulator.service.ScenarioExecutionService.lambda$startScenarioAsync$4(ScenarioExecutionService.java:111) ~[citrus-simulator-starter-1.0.3.jar:na]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_201]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_201]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_201]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_201]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_201]

不幸的是,我无法获得最新版本的柑橘。我的设置在 java 1.8 和 citrus 2.7.1 上运行

有人在使用旧版本的 Citrus Simulator 时遇到过类似的问题吗?

非常感谢您的帮助

最佳答案

我从未调查过此问题的原因(嗯,这显然是一些空 header ),但一个简单的解决方法(不是解决方案!)是覆盖 com.consol .citrus.simulator.serviceMessageService bean如下:

@Service
@Primary
@Transactional
public class NullHeaderAwareMessageService extends MessageService {

private final MessageRepository messageRepository;

@Autowired
public NullHeaderAwareMessageService(MessageRepository messageRepository) {
super(messageRepository);

this.messageRepository = messageRepository;
}

@Override
public Message saveMessage(Message.Direction direction, String payload, String citrusMessageId, Map<String, Object> headers) {
Message message = new Message();
message.setDate(new Date());
message.setDirection(direction);
message.setPayload(payload);
message.setCitrusMessageId(citrusMessageId);
if (headers != null) {
headers.entrySet().stream()
.filter(headerEntry -> headerEntry.getValue() != null)
.forEach(headerEntry -> message.addHeader(
new MessageHeader(headerEntry.getKey(),
StringUtils.abbreviate(headerEntry.getValue().toString(), 255))));
}

return messageRepository.save(message);
}
}

关于java - Citrus Simulator 在接收序列化 xml 数据时抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58031451/

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