我可以创建一个通用的 Web 服务/调度方法来响应所有使用 JAX-WS 的请求吗?

发布于 2024-11-29 06:06:23 字数 2394 浏览 2 评论 0原文

我正在尝试创建一个通用的 Web 服务,无论请求的标头或正文内容如何,​​它都始终以“OK”响应。我可以在 Axis2 中使用 RawXMLInOutMessageReceiver 来完成此操作,但如果可能的话,我更愿意使用 JAX-WS(我对此完全陌生)。到目前为止,我已经有了一个简单的接口:

@WebService
public interface DummyService {
    @WebMethod String processMessage(Object obj);
}

和一个简单的实现:

@WebService(endpointInterface = "com.dummyservice.DummyService")
public class DummyServiceImpl implements DummyService {
    @Override
    public String processMessage(Object obj) {
        return "OK";
    }
}

我可以使用 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>

我收到一个 SOAPFault,指出 Cannot find dispatch method for {}derp

是否有可能创建一个通用/哑 Web 服务来使用 JAX-WS 确认所有内容?如果是这样,有人能指出我正确的方向吗?


编辑 感谢 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'm trying to create a generic web service that will always respond with "OK", regardless of the request's header or body contents. 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. 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, e.g.

<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.

Is it even possible to create a generic/dumb web service that will ACK everything with 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:

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风铃鹿 2024-12-06 06:06:23

我希望您的服务需要以下形式:

<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>

?WSDL 添加到您的端点并检查操作输入 XML 类型和命名空间。

您也许可以使用逻辑处理程序javadoc) 将传入请求转换为这种形式- 我没试过。

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.

You might be able to do something with a logical handler (javadoc) to transform the incoming request to this form - I haven't tried.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文