我对网络服务世界比较陌生,我的研究似乎让我困惑而不是启发我,我的问题是我得到了一个库(jar),我必须用一些网络服务功能来扩展它。
这个库将共享给其他开发人员,jar 中的类将具有调用 Web 服务的方法(本质上设置类的属性,执行一些业务逻辑,例如将对象存储在数据库等,并发送回带有这些修改的对象)。我想让对该服务的调用尽可能简单,希望如此简单,以便使用该类的开发人员只需要这样做。
Car c = new Car("Blue");
c.webmethod();
我一直在研究在服务器上使用的 JAX-WS,但在我看来,我不需要在服务器中创建 wsimport
,也不需要在服务器上创建 wsimport
客户端,因为我知道两者都有这些类,所以我只需要服务器和客户端共享的类之间进行一些交互。您认为在类里面进行网络服务和调用有何意义?
最佳答案
我知道您的问题归结为如何从 Java 调用 SOAP (JAX-WS) Web 服务并获取其返回对象。在这种情况下,您有两种可能的方法:
- 通过
wsimport
生成Java类并使用它们;或 - 创建一个 SOAP 客户端:
- 将服务的参数序列化为 XML;
- 通过HTTP操作调用Web方法;和
- 将返回的 XML 响应解析回对象。
关于第一种方法(使用wsimport
):
我看到您已经拥有服务的(实体或其他)业务类,并且事实上 wsimport
生成了一组全新的类(这些类在某种程度上与您已经存在的类重复)有)。
但是,恐怕在这种情况下,您只能:
- 调整(编辑)
wsimport
生成的代码,使其使用您的业务类(这很困难,而且不值得 - 请记住每次 WSDL 发生变化时,您必须重新生成并重新调整代码);或 - 放弃并使用
wsimport
生成的类。 (在此解决方案中,您的业务代码可以“使用”生成的类作为来自另一个体系结构层的服务。)
关于第二种方法(创建自定义 SOAP 客户端):
为了实现第二种方法,您必须:
- 调用电话:
- 使用 SAAJ(带有 Attachments API for Java 的 SOAP)框架(见下文,它随 Java SE 1.6 或更高版本一起提供)进行调用;或
- 您还可以通过
java.net.HttpUrlconnection
(以及一些java.io
处理)来完成此操作。
- 将对象转换为 XML 或从 XML 转换回来:
- 使用 OXM(对象到 XML 映射)框架(例如 JAXB)将 XML 序列化到对象或将 XML 反序列化到对象中
- 或者,如果必须的话,手动创建/解析 XML(如果接收到的对象与发送的对象只有一点点不同,这可能是最佳解决方案)。
使用经典的 java.net.HttpUrlConnection
创建 SOAP 客户端并不难(但也不是那么简单),您可以在 this link 中找到。一个非常好的起始代码。
我建议您使用SAAJ框架:
SOAP with Attachments API for Java (SAAJ) is mainly used for dealing directly with SOAP Request/Response messages which happens behind the scenes in any Web Service API. It allows the developers to directly send and receive soap messages instead of using JAX-WS.
请参阅下面使用 SAAJ 进行 SOAP Web 服务调用的工作示例(运行它!)。它调用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:
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/ .
关于java - 如何从 Java 类进行 SOAP Web 服务调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56252602/