下面是Http Camel SU的使用HTTP消息的代码段。您能否告知SMSProcessor
组件出了什么问题?
我得到:cannot cast apache.servicemix.jbi.jaxp.StringSource to apache.servicemix.jbi.jaxp.StringSource"
捆绑:
<beans xmlns:http="http://servicemix.apache.org/http/1.0"
xmlns:b="http://rhinopay.com/bridge">
<http:consumer service="b:http"
endpoint="endpoint"
targetService="b:pipeline"
locationURI="http://localhost:8192/rhinopay/"
defaultMep="http://www.w3.org/2004/08/wsdl/in-out"
marshaler="#myMarshaler"
/>
<bean id="myMarshaler" class="marshaller.HttpMarshaller"/>
</beans>
HttpMarshaller:
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.NormalizedMessage;
import javax.servlet.http.HttpServletRequest;
import org.apache.servicemix.common.EndpointComponentContext;
import org.apache.servicemix.http.endpoints.DefaultHttpConsumerMarshaler;
import org.apache.servicemix.jbi.jaxp.StringSource;
public class HttpMarshaller extends DefaultHttpConsumerMarshaler {
public MessageExchange createExchange(HttpServletRequest request,
javax.jbi.component.ComponentContext context) throws Exception {
// TODO Auto-generated method stub
String mobile = request.getParameter("mobile");
String smsTxt = request.getParameter("smsTxt");
// String message = request.getParameter("msg");
MessageExchange exchange = ((EndpointComponentContext) context).getDeliveryChannel().createExchangeFactory().createExchange(getDefaultMep());
NormalizedMessage in = exchange.createMessage();
String xmlContext = mobile+","+smsTxt;
System.out.println("xmlContext---"+xmlContext);
in.setContent(new StringSource(xmlContext));
exchange.setMessage(in,"in");
return exchange;
}
}
SMSProcessor:
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.servicemix.jbi.jaxp.StringSource;
public class SMSProcessor implements Processor {
/*
* @see org.apache.camel.Processor#process(org.apache.camel.Exchange)
*/
public void process(Exchange exchange) throws Exception {
System.out.println("SMSProcessor");
StringSource text = ((StringSource)exchange.getIn().getBody());
System.out.println("text"+text.getText());
}
}
最佳答案
使用Camel类型转换器,而不是Java类型转换
StringSource文本= exchange.getIn()。getBody(StringSource.class);
或者,如果您想要纯文本,则可以
字符串文本= exchange.getIn()。getBody(String.class);
如果你想要一个DOM
文档dom = exchange.getIn()。getBody(Document.class);
等等