Java SOAP Web服务始终返回null

时间:2011-06-01 02:29:56

标签: java soap

我有一个简单的SOAP webservice服务器,如下所示:

public class receiver extends JAXMServlet implements ReqRespListener {

    public SOAPMessage onMessage(SOAPMessage soapm) {
        return soapm;
    }
}

在客户端,我向此服务器发送消息:

public class sender {

    /** This is a sample web service operation */

    SOAPConnectionFactory scfac = null;
    SOAPConnection con =null;
    MessageFactory fac = null;
    SOAPMessage message = null;
    SOAPMessage response = null;
    @WebMethod(operationName = "sender")
    public String sender(@WebParam(name = "a") String a) 
    {
        try{
        //Creat Connection
        scfac = SOAPConnectionFactory.newInstance();
        con = scfac.createConnection();
        fac = MessageFactory.newInstance();
        message = fac.createMessage();
        SOAPPart soapPart = message.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        SOAPHeader header = envelope.getHeader();
        header.detachNode();
        SOAPBody body = envelope.getBody();
        body.addTextNode(a);
        URL endpoint = new URL("http://localhost:8080/Target/receiver");
        //log("Bat dau gui");
        response = con.call(message, endpoint);
        //log("Da nhan ve");
        SOAPPart sp = response.getSOAPPart();
        SOAPEnvelope ev = sp.getEnvelope();
        SOAPBody bd = ev.getBody();
        String result = bd.getValue();
        return result;
        }
        catch (Exception e)
        {
            String fail = "Fail to send";
            return  fail;
        }
}
}

我在jsp文件中调用了sender函数并将一个String传递给它。 我用Netbeans和Tomcat服务器构建它,但它只是响应一条空消息。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我已经通过两个步骤解决了问题:

  • SOAP服务器:在web.xml文件中,将 com.sun.xml.ws.transport.http.servlet.WSServlet 中的servlet类修复到您自己的Java类(在我的例子中)是receive.receiver)
  • 将saaj-impl-1.3.1库添加到NetBeans(使用方法 com.sun.xml.messaging 的库)