gpt4 book ai didi

java - Camel CXF 端点不返回 POJO

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

如何使 Camel cxfEndpoint 返回 POJO?目前它返回 MessageContentsList,其中包含作为 String 成员的 SOAP 消息字段。我希望端点返回一个 POJO,它是使用 cxf-codegen-plugin 从 wsdl 生成的。请注意,提供给端点的服务类是使用相同的工具生成的,并使用了这些类。

这是我正在使用的 Camel 上下文和路由。

<!-- this is a spring XML file where we have Camel embedded -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<bean class="org.apache.camel.component.cxf.transport.CamelTransportFactory" lazy-init="false">
<property name="bus" ref="cxf"/>
<property name="camelContext" ref="camelContext"/>
<property name="checkException" value="true"/>
<property name="transportIds">
<list>
<value>http://cxf.apache.org/transports/camel</value>
</list>
</property>
</bean>

<bean id="messageLoggerBean" class="tutoivon.api.camel.MessageLoggerBean" />

<bean id="messageConverterBean" class="tutoivon.api.camel.MessageConverterBean" />

<cxf:cxfEndpoint id="endpoint" address="http://localhost:1010/hello"
serviceClass="net.webservicex.GlobalWeatherSoap" wsdlURL="META-INF/globalweather.wsdl">
</cxf:cxfEndpoint>

<!-- Here we define Camel, notice the namespace it uses -->
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">

<!-- Camel route to move messages from the ActiveMQ inbox to its outbox
queue -->
<route id="cxfToJMSRoute">
<from uri="cxf:bean:endpoint?dataFormat=POJO" />
<log message="test" />
<to uri="bean:messageConverterBean" />
<!-- <convertBodyTo type="String"/> -->
<wireTap uri="direct:logInfo" />
<to uri="activemq:queue:api" />
</route>

<route id="loggingRoute">
<from uri="direct:logInfo" />
<to uri="bean:messageLoggerBean" />
</route>

</camelContext>

<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL" value="tcp://127.0.0.1:61616"/>
</bean>

</beans>

这里是处理消息的 MessageConverterBean

public class MessageConverterBean implements Processor {

@Override
public void process(Exchange e) throws Exception {
GetCitiesByCountry body = e.getIn().getBody(GetCitiesByCountry.class);

System.out.println("maa: " + body.getCountryName());

e.getIn().setBody(body, GetCitiesByCountry.class);
}

}

但是,正文无法转换为 POJO,因为会抛出以下异常。

org.apache.camel.TypeConversionException: Error during type conversion from type: org.apache.cxf.message.MessageContentsList to the required type: net.webservicex.GetCitiesByCountry with value [Finland] due null
at org.apache.camel.converter.jaxb.FallbackTypeConverter.convertTo(FallbackTypeConverter.java:166) ~[camel-jaxb-2.20.1.jar!/:2.20.1]
at org.apache.camel.impl.converter.BaseTypeConverterRegistry.doConvertTo(BaseTypeConverterRegistry.java:366) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.impl.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:141) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.impl.MessageSupport.getBody(MessageSupport.java:87) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.impl.MessageSupport.getBody(MessageSupport.java:61) ~[camel-core-2.20.1.jar!/:2.20.1]
at tutoivon.api.camel.MessageConverterBean.process(MessageConverterBean.java:12) ~[classes!/:1.0-SNAPSHOT]
at org.apache.camel.component.bean.AbstractBeanProcessor.process(AbstractBeanProcessor.java:108) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:53) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.component.bean.BeanProducer.process(BeanProducer.java:41) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:138) [camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:101) [camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.component.cxf.CxfConsumer$CxfConsumerInvoker.asyncInvoke(CxfConsumer.java:203) [camel-cxf-2.20.1.jar!/:2.20.1]
at org.apache.camel.component.cxf.CxfConsumer$CxfConsumerInvoker.invoke(CxfConsumer.java:180) [camel-cxf-2.20.1.jar!/:2.20.1]

这里是处理器可用的实际消息负载的内容。

enter image description here

这是我要发送的消息。

enter image description here

如何让 Camel 将消息转换为 POJO?这是我所有的依赖项。

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.2.1</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.4.6.v20170531</version>
</dependency>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf-transport</artifactId>
<version>2.20.1</version>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-kahadb-store</artifactId>
</dependency>
</dependencies>

最佳答案

您不需要 CxfTransport,也不需要指定 dataFormat=POJO,因为这已经是默认数据格式。您的类应该在您的服务类 (net.webservicex.GlobalWeatherSoap) 中进行 JAXB 注释和引用。

这是它的外观示例:

国家.java:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "GetCitiesByCountry")
@XmlAccessorType(XmlAccessType.FIELD)
public class GetCitiesByCountry {
String CountryName;
}

GlobalWeatherSoap.java:

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jwx.WebResult;

@WebService (targetNamespace="myNamespace")
public interface GlobalWeatherSoap {
@WebMethod
@WebResult(name = "GetCitiesByCountryResponse", targetNamespace = "myNamespace")
public GetCitiesByCountryResponse GetCitiesByCountry(
@WebParam(name = "GetCitiesByCountry", targetNamespace = "myNamespace")
GetCitiesByCountry country
)
}

显然,您将“myNamespace”替换为您的 WSDL 中使用的那个。

关于java - Camel CXF 端点不返回 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47551481/

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