开发者

Can I create a generic web service/dispatch method that responds to ALL requests with JAX-WS?

开发者 https://www.devze.com 2023-03-27 04:06 出处:网络
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\'

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消