我最近重新学习了Java,所以可能我的代码中有很多愚蠢的错误。我的目标是制作一个向服务器发送SOAP请求的servlet。我使用这个示例来创建SOAP客户机。我将它添加到servlet类中。这显然行不通。你能给我一些技巧来改进这个servlet并理解servlet的逻辑吗?
下面是相关的代码片段:
public class SOAPRequest extends HttpServlet {
private static final long serialVersionUID = 1L;
public SOAPRequest() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SOAPConnectionFactory myFct = SOAPConnectionFactory.newInstance();
SOAPConnection myCon = myFct.createConnection();
MessageFactory msgFct = MessageFactory.newInstance();
SOAPMessage msg = msgFct.createMessage();
SOAPPart mySPart = msg.getSOAPPart();
SOAPEnvelope myEnvp = mySPart.getEnvelope();
SOAPBody body = myEnvp.getBody();
Name bodyName = envelope.createName("GetLastTradePrice", "m", "http://eztrade.com");
SOAPBodyElement gltp = body.addBodyElement(bodyName);
Name myContent = envelope.createName("symbol");
SOAPElement mySymbol = gltp.addChildElement(myContent);
mySymbol.addTextNode("SUNW");
message.saveChanges();
URLEndpoint endPt = new URLEndpoint("http://eztrade.com//quotes");
SOAPMessage reply = myCon.call(message, endPt);
myCon.close();
}
在类路径中是否有所需的jar文件?代码表明您正在使用SAAJ。请获取最新的jar文件,它应该可以工作。
<!-- https://mvnrepository.com/artifact/org.apache.axis/axis-saaj -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>