javascript
maven与spring_与Spring和Maven签约首个SOAP服务
maven與spring
1.簡介
在本教程中,我們將學習使用JAX-WS,Spring和Maven實施合同優先的SOAP服務應用程序。 這是使用合同優先還是代碼優先方法的更多設計決定。
在開發基于SOAP的Web服務應用程序時使用應用合同優先的方法最顯著的好處是,可以在對合同進行必要的更改后立即與消費者/客戶共享合同,因此客戶應用程序和Web服務操作實施可以由不同團隊同時獨立完成,從而節省了大量時間。
2.實施
上面的場景是這樣的場景,客戶端/消費者應用程序將通過服務端點與我們的示例基于SOAP的JAX-WS Web服務示例進行交互。
首先從實現開始,首先在Eclipse中創建一個Maven項目,并確保我們繼續使用與下面所示內容相似的目錄結構。
首先是查看我們的pom依賴關系,它應該類似于:
pom.xml
<dependencies><!-- Spring dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.1.RELEASE</version></dependency><!-- JAX-WS dependencies --><dependency><groupId>org.jvnet.jax-ws-commons.spring</groupId><artifactId>jaxws-spring</artifactId><version>1.9</version></dependency><dependency><groupId>com.sun.xml.ws</groupId><artifactId>jaxws-rt</artifactId><version>2.2.8</version></dependency> </dependencies> <build><finalName>SOAPWebServiceExample</finalName><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>jaxws-maven-plugin</artifactId><version>1.12</version><configuration><wsdlDirectory>${basedir}/src/main/resources/wsdl</wsdlDirectory><packageName>com.jcombat.ws</packageName><keep>true</keep><sourceDestDir>${basedir}/target/generated/src/main/java</sourceDestDir></configuration><executions><execution><id>wsdl_import</id><goals><goal>wsimport</goal></goals></execution></executions></plugin></plugins> </build>請注意我們將使用的wsimport工具,以便稍后從WSDL文件生成存根文件。
現在讓我們檢查一下web.xml文件。
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>SOAPWebServiceExample</display-name><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>customer</servlet-name><servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>customer</servlet-name><url-pattern>/customer</url-pattern></servlet-mapping></web-app>接下來,我們為我們的Web服務創建一個模式文件 。 將其命名為customerService.xsd ,這將基本上為我們將要創建的WSDL文件或Web服務合同定義構造塊。
customerService.xsd
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ws.jcombat.com/"xmlns:tns="http://ws.jcombat.com/" elementFormDefault="qualified"><element name="CustomerServiceRequest" type="tns:CustomerServiceRequestType"></element><complexType name="CustomerServiceRequestType"><sequence><element name="customerId" type="int"></element></sequence></complexType><complexType name="CustomerServiceResponseType"><sequence><element name="customer" type="tns:Customer" maxOccurs="unbounded"minOccurs="0"></element></sequence></complexType><element name="CustomerServiceResponse" type="tns:CustomerServiceResponseType"></element><complexType name="Customer"><sequence><element name="id" type="int" maxOccurs="1" minOccurs="1"></element><element name="name" type="string" maxOccurs="1" minOccurs="1"></element></sequence></complexType> </schema>使用我們剛創建的XML模式創建一個有效的WSDL文件。
customerService.wsdl
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitionsxmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="http://ws.jcombat.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.jcombat.com/"name="customerService"><wsdl:types><xsd:schema targetNamespace="http://ws.jcombat.com/"><xsd:import namespace="http://ws.jcombat.com/"schemaLocation="../schema/customerService.xsd" /></xsd:schema></wsdl:types><wsdl:message name="CustomerServiceRequest"><wsdl:part name="CustomerServiceRequest" element="tns:CustomerServiceRequest" /></wsdl:message><wsdl:message name="CustomerServiceResponse"><wsdl:part name="CustomerServiceResponse" element="tns:CustomerServiceResponse" /></wsdl:message><wsdl:portType name="CustomerServicePortType"><wsdl:operation name="getCustomer"><wsdl:input name="CustomerServiceRequest" message="tns:CustomerServiceRequest" /><wsdl:output name="CustomerServiceResponse" message="tns:CustomerServiceResponse" /></wsdl:operation></wsdl:portType><wsdl:binding name="CustomerEndpointPortBinding" type="tns:CustomerServicePortType"><soap:binding style="document"transport="http://schemas.xmlsoap.org/soap/http" /><wsdl:operation name="getCustomer"><soap:operation style="document" soapAction="getCustomer" /><wsdl:input name="CustomerServiceRequest"><soap:body use="literal" /></wsdl:input><wsdl:output name="CustomerServiceResponse"><soap:body use="literal" /></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="customerService"><wsdl:port name="CustomerEndpointPort" binding="tns:CustomerEndpointPortBinding"><soap:address location="http://localhost:8080/SOAPWebServiceExample/customer" /></wsdl:port></wsdl:service> </wsdl:definitions>接下來是從WSDL文件生成存根文件。 為此,請在系統的命令提示符下運行以下命令。
mvn clean install在/ target目錄中簽出生成的存根文件。
現在,在名為CustomerServiceImpl的類中創建生成的服務接口CustomerServicePortType的實現。
CustomerServiceImpl.java
package com.jcombat.service;import javax.jws.WebService;import com.jcombat.ws.Customer; import com.jcombat.ws.CustomerServicePortType; import com.jcombat.ws.CustomerServiceRequestType; import com.jcombat.ws.CustomerServiceResponseType;@WebService(endpointInterface="com.jcombat.ws.CustomerServicePortType") public class CustomerServiceImpl implements CustomerServicePortType {public CustomerServiceResponseType getCustomer(CustomerServiceRequestType customerServiceRequest) {final CustomerServiceResponseType response = new CustomerServiceResponseType();Customer customer = new Customer();customer.setId(123);customer.setName("Ramesh");response.getCustomer().add(customer);return response;}}必須將@WebService批注應用于端點接口實現類,才能將其標記為Web服務端點。 @WebService批注告訴服務器運行時環境將該類的所有公共方法公開為Web服務方法。
我們已經完成了該應用程序。 最終檢查是,當點擊以下端點URI時,是否可以看到顯示的WSDL內容,我們已將其指定的位置指向WSDL文件的底部。
- http:// localhost:8080 / SOAPWebServiceExample / customer?wsdl
下面是我們在瀏覽器中看到的內容。
是的,我們成功做到了。
3.運行應用程序
使用WSDL URI(http:// localhost:8080 / SOAPWebServiceExample / customer?wsdl)在SOAP UI中設置SOAP項目。
下面是當我們在SOAP UI中實際命中服務時看到的內容。
4.下載源代碼
- 下載源代碼
翻譯自: https://www.javacodegeeks.com/2016/02/contract-first-soap-service-spring-maven.html
maven與spring
總結
以上是生活随笔為你收集整理的maven与spring_与Spring和Maven签约首个SOAP服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: apache ignite_从In Me
- 下一篇: 炖蛋要多长时间 炖蛋要多长时间熟