[英]Can I create a generic web service/dispatch method that responds to ALL requests with JAX-WS?
I'm trying to create a generic web service that will always respond with "OK", regardless of the request's header or body contents.我正在尝试创建一个通用的 web 服务,无论请求的 header 或正文内容如何,该服务将始终以“OK”响应。 I can do this in Axis2 with a
RawXMLInOutMessageReceiver
, but I'd prefer to use JAX-WS (which I am completely new to) if at all possible.我可以在 Axis2 中使用
RawXMLInOutMessageReceiver
执行此操作,但如果可能的话,我更喜欢使用 JAX-WS(我完全不熟悉)。 So far I've got a simple interface:到目前为止,我有一个简单的界面:
@WebService
public interface DummyService {
@WebMethod String processMessage(Object obj);
}
and a simple implementaion:和一个简单的实现:
@WebService(endpointInterface = "com.dummyservice.DummyService")
public class DummyServiceImpl implements DummyService {
@Override
public String processMessage(Object obj) {
return "OK";
}
}
I can successfully publish the service with javax.xml.ws.Endpoint#publish(...)
, but when I hit it with a simple SOAP request, eg我可以使用
javax.xml.ws.Endpoint#publish(...)
成功发布服务,但是当我用一个简单的 SOAP 请求点击它时,例如
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<derp/>
</soapenv:Body>
</soapenv:Envelope>
I'm greeted with a SOAPFault stating Cannot find dispatch method for {}derp
.我收到了一个 SOAPFault 表示
Cannot find dispatch method for {}derp
。
Is it even possible to create a generic/dumb web service that will ACK everything with JAX-WS?是否甚至可以创建一个通用/哑 web 服务,它将使用 JAX-WS 确认所有内容? If so, could someone point me in the right direction?
如果是这样,有人可以指出我正确的方向吗?
EDIT Thanks to the tip from McDowell, I was able to do this with a SOAPHandler
:编辑感谢 McDowell 的提示,我能够使用
SOAPHandler
做到这一点:
public class DummySOAPHandler implements SOAPHandler {
@Override
public boolean handleMessage(MessageContext context) {
return process((SOAPMessageContext) context);
}
@Override
public boolean handleFault(MessageContext context) {
return process((SOAPMessageContext) context);
}
@Override
public void close(MessageContext context) { }
@Override
public Set<QName> getHeaders() {
return null;
}
private boolean process(SOAPMessageContext ctx) {
try {
SOAPMessage message = ctx.getMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPBody body = message.getSOAPBody();
if ((Boolean) ctx.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
Iterator<SOAPElement> bodyChildren = body.getChildElements();
while (bodyChildren.hasNext()) {
SOAPElement child = bodyChildren.next();
child.detachNode();
}
body.addBodyElement(envelope.createName("OK"));
message.saveChanges();
}
} catch (SOAPException e) {
e.printStackTrace();
}
return true;
}
}
I expect your service is expecting something of the form:我希望您的服务期待以下形式:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:dum="http://yournamespace/">
<soapenv:Header/>
<soapenv:Body>
<dum:processMessage>
<!-- xsd:anyType -->
</dum:processMessage>
</soapenv:Body>
</soapenv:Envelope>
Add ?WSDL
to your endpoint and inspect the operation input XML type and the namespaces.将
?WSDL
添加到您的端点并检查操作输入 XML 类型和命名空间。
You might be able to do something with a logical handler ( javadoc ) to transform the incoming request to this form - I haven't tried.您可能可以使用逻辑处理程序( javadoc ) 将传入请求转换为这种形式 - 我没有尝试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.