javascript
maven与spring_与Spring和Maven签约首个SOAP服务
maven與spring
1.簡(jiǎn)介
在本教程中,我們將學(xué)習(xí)使用JAX-WS,Spring和Maven實(shí)施合同優(yōu)先的SOAP服務(wù)應(yīng)用程序。 這是使用合同優(yōu)先還是代碼優(yōu)先方法的更多設(shè)計(jì)決定。
在開發(fā)基于SOAP的Web服務(wù)應(yīng)用程序時(shí)使用應(yīng)用合同優(yōu)先的方法最顯著的好處是,可以在對(duì)合同進(jìn)行必要的更改后立即與消費(fèi)者/客戶共享合同,因此客戶應(yīng)用程序和Web服務(wù)操作實(shí)施可以由不同團(tuán)隊(duì)同時(shí)獨(dú)立完成,從而節(jié)省了大量時(shí)間。
2.實(shí)施
上面的場(chǎng)景是這樣的場(chǎng)景,客戶端/消費(fèi)者應(yīng)用程序?qū)⑼ㄟ^服務(wù)端點(diǎn)與我們的示例基于SOAP的JAX-WS Web服務(wù)示例進(jìn)行交互。
首先從實(shí)現(xiàn)開始,首先在Eclipse中創(chuàng)建一個(gè)Maven項(xiàng)目,并確保我們繼續(xù)使用與下面所示內(nèi)容相似的目錄結(jié)構(gòu)。
首先是查看我們的pom依賴關(guān)系,它應(yīng)該類似于:
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>請(qǐng)注意我們將使用的wsimport工具,以便稍后從WSDL文件生成存根文件。
現(xiàn)在讓我們檢查一下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>接下來,我們?yōu)槲覀兊腤eb服務(wù)創(chuàng)建一個(gè)模式文件 。 將其命名為customerService.xsd ,這將基本上為我們將要?jiǎng)?chuàng)建的WSDL文件或Web服務(wù)合同定義構(gòu)造塊。
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>使用我們剛創(chuàng)建的XML模式創(chuàng)建一個(gè)有效的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文件生成存根文件。 為此,請(qǐng)?jiān)谙到y(tǒng)的命令提示符下運(yùn)行以下命令。
mvn clean install在/ target目錄中簽出生成的存根文件。
現(xiàn)在,在名為CustomerServiceImpl的類中創(chuàng)建生成的服務(wù)接口CustomerServicePortType的實(shí)現(xiàn)。
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批注應(yīng)用于端點(diǎn)接口實(shí)現(xiàn)類,才能將其標(biāo)記為Web服務(wù)端點(diǎn)。 @WebService批注告訴服務(wù)器運(yùn)行時(shí)環(huán)境將該類的所有公共方法公開為Web服務(wù)方法。
我們已經(jīng)完成了該應(yīng)用程序。 最終檢查是,當(dāng)點(diǎn)擊以下端點(diǎn)URI時(shí),是否可以看到顯示的WSDL內(nèi)容,我們已將其指定的位置指向WSDL文件的底部。
- http:// localhost:8080 / SOAPWebServiceExample / customer?wsdl
下面是我們?cè)跒g覽器中看到的內(nèi)容。
是的,我們成功做到了。
3.運(yùn)行應(yīng)用程序
使用WSDL URI(http:// localhost:8080 / SOAPWebServiceExample / customer?wsdl)在SOAP UI中設(shè)置SOAP項(xiàng)目。
下面是當(dāng)我們?cè)赟OAP UI中實(shí)際命中服務(wù)時(shí)看到的內(nèi)容。
4.下載源代碼
- 下載源代碼
翻譯自: https://www.javacodegeeks.com/2016/02/contract-first-soap-service-spring-maven.html
maven與spring
總結(jié)
以上是生活随笔為你收集整理的maven与spring_与Spring和Maven签约首个SOAP服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: apache ignite_从In Me
- 下一篇: spring多个视图解析器_在Sprin