SOAP 요청은 서버로 보내는 매개 변수로 구성된 XML 파일입니다.
SOAP 응답은 XML 파일이지만 이제는 서비스에서 제공하려는 모든 정보가 포함되어 있습니다.
기본적으로 WSDL은이 두 XML의 구조를 설명하는 XML 파일입니다.
자바 간단한 SOAP 클라이언트를 구현하려면, 당신은 SAAJ 프레임 워크를 사용할 수 있습니다 (위의 JSE 1.6과 함께 제공됩니다) :
SOAP를 첨부 파일 API와 자바 (SAAJ)에 대한입니다 주로 웹 서비스 API의 무대 뒤에서 발생하는 SOAP 요청/응답 메시지를 직접 처리하는 데 사용됩니다. 개발자는 JAX-WS를 사용하는 대신 비누 메시지를 직접 보내고받을 수 있습니다.
SAAJ를 사용하는 SOAP 웹 서비스 호출의 작동 예 (실행!)를 참조하십시오. 전화는 this web service입니다.
import javax.xml.soap.*;
public class SOAPClientSAAJ {
// SAAJ - SOAP Client Testing
public static void main(String args[]) {
/*
The example below requests from the Web Service at:
http://www.webservicex.net/uszip.asmx?op=GetInfoByCity
To call other WS, change the parameters below, which are:
- the SOAP Endpoint URL (that is, where the service is responding from)
- the SOAP Action
Also change the contents of the method createSoapEnvelope() in this class. It constructs
the inner part of the SOAP envelope that is actually sent.
*/
String soapEndpointUrl = "http://www.webservicex.net/uszip.asmx";
String soapAction = "http://www.webserviceX.NET/GetInfoByCity";
callSoapWebService(soapEndpointUrl, soapAction);
}
private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();
String myNamespace = "myNamespace";
String myNamespaceURI = "http://www.webserviceX.NET";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);
/*
Constructed SOAP Request Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="http://www.webserviceX.NET">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<myNamespace:GetInfoByCity>
<myNamespace:USCity>New York</myNamespace:USCity>
</myNamespace:GetInfoByCity>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("GetInfoByCity", myNamespace);
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("USCity", myNamespace);
soapBodyElem1.addTextNode("New York");
}
private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println();
soapConnection.close();
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
soapMessage.saveChanges();
/* Print the request message, just for debugging purposes */
System.out.println("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println("\n");
return soapMessage;
}
}
wsdl 파일이 있으면 Java 클래스를 생성하여 대신 사용할 수 있습니다. – RandomQuestion
[this stackoverflow topic] (http://stackoverflow.com/questions/19274828/how-to-use-wsdl/19276139#19276139)를보십시오. 나는 당신에게 유용 할 수있는 몇 가지 링크를 게시합니다. – Paolo
예, 당신이 말하고있는 항아리는 혼자서 만들 수 있습니다. wsdport에서 javdocs를 찾아 클라이언트를 생성하십시오. 필요한 경우 항아리로 만들 수 있습니다.javadoc : http : //docs.oracle.com/javase/7/docs/technotes/tools/share/wsimport.html, 예 : http : //www.mkyong.com/webservices/jax-ws/jax-ws- wsimport-tool-example/ –