2016-04-26 132 views
1

我想使用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> 

你能幫我出示ŧ他在回覆中返回值? 謝謝

回答

1

你應該返回一個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

+0

感謝您的答覆。我看了一下文檔和鏈接。主要關心的是我必須爲每個方法創建一個payloadName。我想知道是否可以從Web服務自動生成SOAP響應(包裝消息中的返回字符串) – user2824073

+1

@ user2824073,這將打破SOA原則。應該明確定義每種方法並進行記錄,以便生成的WSDL可以由開發人員以最小的努力使用。根據我對你評論的理解,你想要一種方法會返回一個不同的答案?請澄清 – Namphibian