使用Camel实现CXF Web服务
问题描述:
我想使用Camel CXF组件公开Code First Web服务。通过装配了一些可用的例子,我得出以下路由定义:使用Camel实现CXF Web服务
<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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
<bean id="productServiceImpl" class="com.demo.ws.CustomerServiceImpl" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="cxf:bean:productServiceEndpoint" />
<bean ref="productServiceImpl" />
<!-- log input received -->
<to uri="log:output" />
</route>
</camelContext>
<cxf:cxfEndpoint id="productServiceEndpoint"
address="http://localhost:9001/productService" serviceClass="com.demo.ws.CustomerService" />
</beans>
我使用的SEI和实现类是微不足道的:
@WebService(serviceName="customerService")
public interface CustomerService
{
public String getCustomerById(String customerId);
}
public class CustomerServiceImpl implements CustomerService
{
@Override
public String getCustomerById(String customerId)
{
System.out.println("Called with "+customerId);
return "Hello " +customerId;
}
}
当运行项目时,但是web服务的实现类是正确调用,返回字符串“Hello [名]”,从SOAPUI返回的身体是空的:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body/>
</soap:Envelope>
你能帮我出示ŧ他在回复中返回值? 谢谢
答
你应该返回一个SOAP消息:
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
QName payloadName = new QName("http://apache.org/hello_world_soap_http/types", "greetMeResponse", "ns1");
SOAPBodyElement payload = body.addBodyElement(payloadName);
SOAPElement message = payload.addChildElement("responseType");
message.addTextNode("Your custom message");
return soapMessage;
您还可以看看骆驼DOC例子:http://camel.apache.org/cxf-example.html
感谢您的答复。我看了一下文档和链接。主要关心的是我必须为每个方法创建一个payloadName。我想知道是否可以从Web服务自动生成SOAP响应(包装消息中的返回字符串) – user2824073
@ user2824073,这将打破SOA原则。应该明确定义每种方法并进行记录,以便生成的WSDL可以由开发人员以最小的努力使用。根据我对你评论的理解,你想要一种方法会返回一个不同的答案?请澄清 – Namphibian