问题
我对网络服务世界相对较新,我的研究似乎让我困惑不仅仅是启发我,我的问题是我得到了一个库(jar),我必须扩展一些webservice功能。
这个库将与其他开发人员共享,并且jar中的类将是具有调用webservice的方法的类(实质上设置类的属性,执行一些业务逻辑,如将对象存储在db中,等,并通过这些修改发回对象)。我希望尽可能简单地调用此服务,希望这样简单,以便开发人员只需要使用该类。
Car c = new Car("Blue");
c.webmethod();
我一直在研究JAX-WS在服务器上使用,但在我看来,我不需要在服务器上创建awsimport
,也不需要在客户端创建wsimport
,因为我知道两者都有类,我只需要在类之间进行一些交互服务器和客户端。你认为在课堂上进行网络服务和通话有何意义?
#1 热门回答(238 赞)
我理解你的问题归结为如何从Java调用SOAP(JAX-WS)Web服务并获取其返回对象.在这种情况下,你有两种可能的方法:
- 通过wsimport生成Java类并使用它们;要么
- 创建一个SOAP客户端:将服务的参数序列化为XML;通过HTTP操作调用web方法;并将返回的XML响应解析回对象。
关于第一种方法(使用wsimport
):
我看到你已经拥有了服务'(实体或其他)业务类,并且事实上,wsimport
生成了一组全新的类(它们在某种程度上与你已经拥有的类重复)。
但我担心,在这种情况下,你只能:
- 调整(编辑)wsimport生成的代码,使其使用你的业务类(这很困难,不知何故不值得 - 记住每次WSDL更改时,你都必须重新生成并重新编写代码);要么
- 放弃并使用wsimport生成的类。 (在此解决方案中,业务代码可以将生成的类"用作"来自另一个体系结构层的服务。)
关于第二种方法(创建自定义SOAP客户端):
为了实现第二种方法,你必须: - 拨打电话:使用SAAJ(SOAP with Attachments API for Java)框架(见下文,Java SE 1.6或更高版本附带)进行调用;或者你也可以通过java.net.HttpUrlconnection(以及一些java.io处理)来完成。
- 从XML转换对象:使用OXM(对象到XML映射)框架(如JAXB)从/向对象序列化/反序列化XML或者,如果必须,手动创建/解析XML(这可以是如果收到的对象与发送的对象仅有点不同,则为最佳解决方案。
使用classicjava.net.HttpUrlConnection
创建一个SOAP客户端并不那么难(但也不是那么简单),你可以在this linka找到非常好的启动代码。
我建议你使用SAAJ框架:
SOAP with Attachments API for Java(SAAJ)主要用于直接处理任何Web Service API中幕后发生的SOAP请求/响应消息。它允许开发人员直接发送和接收soap消息,而不是使用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/。
#2 热门回答(2 赞)
或者只需使用Apache CXF's 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