#java #soap
#java #soap
Вопрос:
У меня есть простой сервер веб-сервиса SOAP, подобный этому:
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 resu<
}
catch (Exception e)
{
String fail = "Fail to send";
return fail;
}
}
}
Я вызвал функцию sender в файле jsp и передал ей строку.
Я создал его с помощью Netbeans и сервера Tomcat, но он просто отвечает нулевым сообщением.
Как я могу исправить проблему?
Ответ №1:
Я устранил проблему за 2 шага:
- Сервер SOAP: в web.xml файл исправляет класс сервлета из com.sun.xml.ws.transport.http.servlet.WSServlet к вашему собственному классу Java (в моем случае это receive.receiver)
- Добавьте библиотеку saaj-impl-1.3.1 в NetBeans (библиотеку с методом com.sun.xml.messaging)