所以我是整个SOAP和服务器概念的新手.我已经整理了一个基本的JAX-RPC和JAX-WS.我总体上希望将.XML文件传递给Web服务,接收响应并将其写入目录.我从哪里开始,我应该使用什么,以及哪里可以找到基于它的教程/信息.谢谢!
基本上,在设计Web服务时可以采用两种方法.自上而下的方法和自下而上的方法.我将简要介绍这两种方法及其起伏.还有一些教程的链接.
自顶向下:
在自上而下的方法中,您首先建模一个XSD,它将包含您的请求和响应消息以及这些请求和响应将使用的数据结构.然后,您可以对操作建模,即在客户端和服务之间流动的请求和响应,最后将它们组合到一个WSDL中.然后将生成的WSDL导入到Netbeans或Eclipse等IDE中,然后开始编写服务的内部.
例如,假设您有产品服务.在此服务中,您希望创建一个基于产品代码搜索特定产品的操作.因此,您需要在产品服务中查询产品对象.将调用该服务并调用ProductService该操作GetProduct.
要实现此目的,您需要为具有两个名为description和code的字符串属性的产品对象建模.您还需要为GetProductRequest消息和GetProductResponse消息建模.
这可能采取以下结构:

XSD的代码如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns="http://www.wsexample.com/ProductService_V1/Product"
elementFormDefault="qualified"
targetNamespace="http://www.wsexample.com/ProductService_V1/Product"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="ProductCode_Type">
<xs:annotation>
<xs:documentation>This the product code type. It is based on the string data type it must be 8 characters long.
</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:minLength value="8" />
<xs:maxLength value="8" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ProductDescription_Type">
<xs:annotation>
<xs:documentation>This is the base class for the product description field. This is a text field up to 255 characters long.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:maxLength value="255" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="Product_Type">
<xs:annotation>
<xs:documentation>This is the product base class it is used to perform CRUD operations with on all of the product service operations.
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="ProductCode"
type="ProductCode_Type"
minOccurs="1"
maxOccurs="1" />
<xs:element name="ProductDescription"
type="ProductDescription_Type"
minOccurs="0"
maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="GetProductRequest_Type">
<xs:annotation>
<xs:documentation>This is the base class for the Get Product Request message. In the message you must pass one and only one product code which to search for. </xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Product"
type="Product_Type" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="GetProductResponse_Type">
<xs:annotation>
<xs:documentation>This is the get product response message and will contain the result of the results of calling the getproductdescription operation on the Product service.
It will contain a product code which was passed in the Get Product Request message and optionally return one description.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Product"
type="Product_Type" />
</xs:sequence>
</xs:complexType>
<xs:element name="GetProductRequest"
type="GetProductRequest_Type" />
<xs:element name="GetProductResponse"
type="GetProductResponse_Type" />
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
您现在需要创建一个新的WSDL来描述该服务,并在此WSDL中使用此XSD(我称之为product.xsd).正如您所看到的,我们已经建模了数据结构以传输产品对象,并且我们已经模拟了服务中使用的操作.
我们的WSDL可能看起来像这样:

这是WSDL的代码
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="ProductService"
targetNamespace="http://wsexample.com/ProductService"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="http://wsexample.com/ProductService"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:ProductData="http://www.wsexample.com/Product/ProductData">
<wsdl:types>
<xs:schema elementFormDefault="qualified"
targetNamespace="http://wsexample.com/ProductService">
<xs:import schemaLocation="Product.xsd"
namespace="http://www.wsexample.com/Product/ProductData" />
</xs:schema>
</wsdl:types>
<wsdl:message name="GetProduct">
<wsdl:part name="in"
element="ProductData:GetProductRequest" />
</wsdl:message>
<wsdl:message name="GetProductRs">
<wsdl:part name="out"
element="ProductData:GetProductResponse" />
</wsdl:message>
<wsdl:portType name="ProductEndPoint">
<wsdl:operation name="GetProduct">
<wsdl:input message="tns:GetProduct" />
<wsdl:output message="tns:GetProductRs" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ProductServiceBinding"
type="tns:ProductEndPoint">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<wsdl:operation name="GetProduct">
<wsdl:input>
<soap:body parts="in"
use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body parts="out"
use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ProductEndpointService">
<wsdl:port name="ProductServiceEndPointPort"
binding="tns:ProductServiceBinding">
<soap:address location="http://wsexample.com/ProductService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Run Code Online (Sandbox Code Playgroud)
因此,在所有建模之后,将在客户端和服务器之间流动的消息将如下所示:
请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://www.wsexample.com/Product/ProductData">
<soapenv:Header/>
<soapenv:Body>
<prod:GetProductRequest>
<prod:Product>
<prod:ProductCode>12345678</prod:ProductCode>
</prod:Product>
</prod:GetProductRequest>
</soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)
响应:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://www.wsexample.com/Product/ProductData">
<soapenv:Header/>
<soapenv:Body>
<prod:GetProductResponse>
<prod:Product>
<prod:ProductCode>12345678</prod:ProductCode>
<!--Optional:-->
<prod:ProductDescription>A Basic product for kids to teach them how to count. </prod:ProductDescription>
</prod:Product>
</prod:GetProductResponse>
</soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用WSDL和XSD来使用此Netbeans教程和指南来实现Web服务..它非常简单,只需在netbeans中启动一个新的Web项目,然后右键单击该项目并添加新文件,然后从WSDL文件中选择Web服务.
请参见下面的屏幕截图:

自上而下摘要:
自上而下需要您预先进行大量的建模和规划.但是你完全可以控制.这允许您决定数据在客户端和服务器之间的流动方式.它允许您根据您的具体要求修改所有内容.然而,它需要大量的工作.
我最大的好处是我可以设计一个WSDL和XSD,将数据从提供系统抽象为更通用的东西.这在集成项目中变得很重要.
自下而上:
自下而上的方法允许您从java代码生成我在上面创建的工件.这里有一个很好的教程,可以向您展示所有细节.但是,Java允许您向类添加注释,然后将类及其方法公开为Web服务.
因此,通过学习类并添加注释,您可以将该类转换为Web服务.有关快速而肮脏的示例,请参阅下面的代码:
@Webservice
public Class CalculatorWS
{
@WebMethod
public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
int k = i + j;
return k;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您按照本教程学习,您可能会在几分钟内从代码转到完全正常工作的Web服务.快捷方便.
自下而上摘要:
您几乎无法控制WSDL和XSD的外观和行为方式.在某些情况下,这种方法会将您与底层模型紧密绑定.这实际上取决于你如何编码.因此,采用自下而上的方法,您可以在几分钟内获得原型,但上下流动的信息可能与您的想法完全不同.
重要提示:
下载SOAPUI的副本,它确实是用于测试甚至创建模拟服务的最佳工具.如果您认真使用Web服务,请立即获取.
希望这可以帮助你减少兔子洞.