[英]How to do a SOAP Web Service call from Java class?
我對 web 服務世界比較陌生,我的研究似乎讓我感到困惑而不是啟發我,我的問題是我得到了一個庫(jar),我必須用一些 web 服務功能擴展它。
該庫將共享給其他開發人員,並且 jar 中的類將是具有調用 web 服務的方法的類(本質上設置類的屬性,執行一些業務邏輯,例如將對象存儲在 db 中,等,並發送回帶有這些修改的對象)。 我想讓對這個服務的調用盡可能簡單,希望盡可能簡單,以便使用該類的開發人員只需要這樣做。
Car c = new Car("Blue");
c.webmethod();
我一直在研究要在服務器上使用的 JAX-WS,但在我看來,我不需要在服務器上創建wsimport
,也不需要在客戶端上創建wsimport
,因為我知道兩者都有類,我只需要一些交互在服務器和客戶端共享的類之間。 你認為在課堂上做 web 服務和調用有什么意義?
我了解您的問題歸結為如何從 Java 調用 SOAP (JAX-WS) Web 服務並獲取其返回對象。 在這種情況下,您有兩種可能的方法:
wsimport
生成Java類並使用; 或者
關於第一種方法(使用wsimport
):
我看到您已經擁有服務(實體或其他)業務類,事實上wsimport
生成了一組全新的類(它們在某種程度上是您已經擁有的類的重復)。
不過,恐怕在這種情況下,您只能:
wsimport
生成的代碼以使其使用您的業務類(這很困難,而且不知何故不值得 - 請記住,每次 WSDL 更改時,您都必須重新生成並重新調整代碼); 或者wsimport
生成的類。 (在這個解決方案中,您的業務代碼可以“使用”生成的類作為來自另一個架構層的服務。)關於第二種方法(創建您的自定義 SOAP 客戶端):
為了實施第二種方法,您必須:
java.net.HttpUrlconnection
(和一些java.io
處理)來完成。 使用經典的java.net.HttpUrlConnection
創建 SOAP 客戶端並不難(但也不是那么簡單),您可以在此鏈接中找到一個非常好的起始代碼。
我推薦你使用 SAAJ 框架:
SOAP with Attachments API for Java (SAAJ)主要用於直接處理在任何 Web 服務 API 中發生在幕后的 SOAP 請求/響應消息。 它允許開發人員直接發送和接收肥皂消息,而不是使用 JAX-WS。
請參閱下面使用 SAAJ 的 SOAP Web 服務調用的工作示例(運行它!)。 它調用這個網絡服務。
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:
https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit
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 = "https://www.w3schools.com/xml/tempconvert.asmx";
String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit";
callSoapWebService(soapEndpointUrl, soapAction);
}
private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();
String myNamespace = "myNamespace";
String myNamespaceURI = "https://www.w3schools.com/xml/";
// 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="https://www.w3schools.com/xml/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<myNamespace:CelsiusToFahrenheit>
<myNamespace:Celsius>100</myNamespace:Celsius>
</myNamespace:CelsiusToFahrenheit>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("CelsiusToFahrenheit", myNamespace);
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Celsius", myNamespace);
soapBodyElem1.addTextNode("100");
}
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;
}
}
關於使用 JAXB 進行序列化/反序列化,很容易找到有關它的信息。 你可以從這里開始: http ://www.mkyong.com/java/jaxb-hello-world-example/。
或者只是使用Apache CXF 的 wsdl2java來生成您可以使用的對象。
它包含在二進制包中,您可以從他們的網站下載。 您可以簡單地運行如下命令:
$ ./wsdl2java -p com.mynamespace.for.the.api.objects -autoNameResolution http://www.someurl.com/DefaultWebService?wsdl
它使用 wsdl 生成對象,您可以像這樣使用它(對象名稱也從 wsdl 中獲取,因此您的名稱會有所不同):
DefaultWebService defaultWebService = new DefaultWebService();
String res = defaultWebService.getDefaultWebServiceHttpSoap11Endpoint().login("webservice","dadsadasdasd");
System.out.println(res);
甚至還有一個 Maven 插件可以生成源代碼: https ://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html
注意:如果您使用 CXF 和 IDEA 生成源代碼,您可能需要查看: https ://stackoverflow.com/a/46812593/840315
可能對將 xml 請求作為字符串的人有所幫助。 如果您有 WSDL,您可以使用該 WSDL 文件在 SoapUI 中創建一個新的肥皂請求。
它將自動為輸入請求生成結構/XML。
如果您有來自 SoapUI 的輸入請求 xml,則可以使用以下 Java 代碼的一些簡單版本來調用 Soap 服務:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class SimpleSoapClient {
public static void main(String args[]) throws IOException {
String address="Hyderabad";
/* place your xml request from soap ui below with necessary changes in parameters*/
String xml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://www.YourUrlAsPerWsdl.com/\">\r\n" +
" <soapenv:Header/>\r\n" +
" <soapenv:Body>\r\n" +
" <ws:callRest>\r\n" +
" <name>"+"Hello"+"</name>\r\n" +
" <address>"+address+"</address>\r\n" +
" </ws:callRest>\r\n" +
" </soapenv:Body>\r\n" +
"</soapenv:Envelope>";
String responseF=callSoapService(xml);
System.out.println(responseF);
}
}
static String callSoapService(String soapRequest) {
try {
String url = "https://gogle.com/service/hello"; // replace your URL here
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// change these values as per soapui request on top left of request, click on RAW, you will find all the headers
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","text/xml; charset=utf-8");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(soapRequest);
wr.flush();
wr.close();
String responseStatus = con.getResponseMessage();
System.out.println(responseStatus);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// You can play with response which is available as string now:
String finalvalue= response.toString();
// or you can parse/substring the required tag from response as below based your response code
finalvalue= finalvalue.substring(finalvalue.indexOf("<response>")+10,finalvalue.indexOf("</response>")); */
return finalvalue;
}
catch (Exception e) {
return e.getMessage();
}
}
}
我找到了一種更簡單的生成肥皂消息的替代方法。 給定一個 Person 對象:
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Person {
private String name;
private int age;
private String address; //setter and getters below
}
下面是一個簡單的 Soap 消息生成器:
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@Slf4j
public class SoapGenerator {
protected static final ObjectMapper XML_MAPPER = new XmlMapper()
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.registerModule(new JavaTimeModule());
private static final String SOAP_BODY_OPEN = "<soap:Body>";
private static final String SOAP_BODY_CLOSE = "</soap:Body>";
private static final String SOAP_ENVELOPE_OPEN = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
private static final String SOAP_ENVELOPE_CLOSE = "</soap:Envelope>";
public static String soapWrap(String xml) {
return SOAP_ENVELOPE_OPEN + SOAP_BODY_OPEN + xml + SOAP_BODY_CLOSE + SOAP_ENVELOPE_CLOSE;
}
public static String soapUnwrap(String xml) {
return StringUtils.substringBetween(xml, SOAP_BODY_OPEN, SOAP_BODY_CLOSE);
}
}
您可以通過以下方式使用:
public static void main(String[] args) throws Exception{
Person p = new Person();
p.setName("Test");
p.setAge(12);
String xml = SoapGenerator.soapWrap(XML_MAPPER.writeValueAsString(p));
log.info("Generated String");
log.info(xml);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.