webService的简单使用
?
webService是用于解決分布式系統(tǒng)通信的一種解決方案。Apachecxf是當(dāng)前主流的webService開發(fā)框架,由Apache提供。
Apachecxf的webService開發(fā),主要分為兩種服務(wù)提供方式:Ws和Rs。JAX-WS傳輸數(shù)據(jù)使用的是xml格式,基于soap協(xié)議,而JAX-RS傳輸數(shù)據(jù)使用的是xml或者json,基于http協(xié)議。首先,來看一下JAX-WS的獨(dú)立服務(wù)使用:
1.建立一個(gè)maven的java項(xiàng)目
? ? ? ? ?
2.在項(xiàng)目中導(dǎo)入cxf的jar包
? 使用maven的坐標(biāo)
<!-- 使用jax-ws進(jìn)行開發(fā) --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><!-- 內(nèi)置jetty服務(wù)器 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.0.1</version></dependency>3.編寫服務(wù)器端程序
編寫實(shí)體類
package com.xx.cn.cxf_ws__helloworld.domain;public class Car {private Integer id;private String carName;private Double price;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getCarName() {return carName;}public void setCarName(String carName) {this.carName = carName;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}@Overridepublic String toString() {return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";}}package com.xx.cn.cxf_ws__helloworld.domain;import java.util.ArrayList; import java.util.List;public class User {private Integer id;private String username;private String city;private List<Car> cars = new ArrayList<Car>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public List<Car> getCars() {return cars;}public void setCars(List<Car> cars) {this.cars = cars;}}
編寫服務(wù)
package com.xx.cn.cxf_ws__helloworld.service;import java.util.List;import javax.jws.WebMethod; import javax.jws.WebService;import com.xx.cn.cxf_ws__helloworld.domain.Car; import com.xx.cn.cxf_ws__helloworld.domain.User;//編寫webService服務(wù)器端接口@WebService public interface IUserService {@WebMethodpublic String sayHello(String name);@WebMethodpublic List<Car> findCarsByUser(User user); }package com.xx.cn.cxf_ws__helloworld.service;import java.util.ArrayList; import java.util.List;import javax.jws.WebService;import com.xx.cn.cxf_ws__helloworld.domain.Car; import com.xx.cn.cxf_ws__helloworld.domain.User;//編寫webService接口實(shí)現(xiàn)類 @WebService(endpointInterface="com.xx.cn.cxf_ws__helloworld.service.IUserService",serviceName="userService") public class IUserServiceImpl implements IUserService{public String sayHello(String name) {// 簡單參數(shù)傳遞return "Hello" + name;}public List<Car> findCarsByUser(User user) {// 復(fù)雜參數(shù)傳遞List<Car> cars = new ArrayList<Car>();Car car = new Car();car.setId(1);car.setCarName("大眾");car.setPrice(200000d);cars.add(car);user.setCars(cars);return cars;}}
發(fā)布服務(wù)
package com.xx.cn.cxf_ws__helloworld.server;import javax.xml.ws.Endpoint;import com.xx.cn.cxf_ws__helloworld.service.IUserService; import com.xx.cn.cxf_ws__helloworld.service.IUserServiceImpl;//發(fā)布jax_ws服務(wù) public class CXFServer {public static void main(String[] args) {//服務(wù)實(shí)現(xiàn)對象IUserService userService = new IUserServiceImpl();//發(fā)布服務(wù)地址String address = "http://localhost:9909/userService";//發(fā)布服務(wù)Endpoint.publish(address, userService);System.out.println("服務(wù)已經(jīng)啟動(dòng)...");} }服務(wù)訪問地址
http://localhost:9909/userService?wsdl編寫客戶端操作
package com.xx.cn.cxf_ws__helloworld.client;import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import com.xx.cn.cxf_ws__helloworld.service.IUserService;//WS客戶端 public class WSClient {public static void main(String[] args) {//編寫客戶端調(diào)用發(fā)布的webService服務(wù)JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();jaxWsProxyFactoryBean.setAddress("http://localhost:9909/userService");jaxWsProxyFactoryBean.setServiceClass(IUserService.class);//添加日志監(jiān)控jaxWsProxyFactoryBean.getInInterceptors().add(new LoggingInInterceptor());jaxWsProxyFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());//創(chuàng)建遠(yuǎn)程代理對象IUserService proxy = (IUserService) jaxWsProxyFactoryBean.create();//調(diào)用代理對象任何一個(gè)方法,都將通過網(wǎng)絡(luò)調(diào)用web服務(wù)System.out.println(proxy.sayHello("瓊?cè)A派"));} }接下來,介紹一下WS整合spring進(jìn)行開發(fā)
1.建立mavenweb工程
2.基于maven導(dǎo)入坐標(biāo)
<dependencies><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies><build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>tomcat-maven-plugin</artifactId><version>1.1</version><configuration><port>9800</port></configuration></plugin></plugins></build>3.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><!-- spring配置文件位置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- spring核心監(jiān)聽器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>CXFService</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFService</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list></web-app>4.導(dǎo)入先前寫好的domain和service
5.在spring中配置cxf服務(wù)發(fā)布
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><!-- address:客戶端訪問服務(wù)路徑serviceClass:配置接口serviceBean:配置實(shí)現(xiàn)類 --><jaxws:server id="userService" address="/userService" serviceClass="com.xx.cn.cxf_ws__helloworld.service.IUserService"><jaxws:serviceBean><bean class="com.xx.cn.cxf_ws__helloworld.service.IUserServiceImpl" /></jaxws:serviceBean></jaxws:server></beans>訪問地址
http://localhost:9800/cxf_ws__spring/services/userService?wsdl6.整合spring,編寫客戶端
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><jaxws:client id="userServiceClient" serviceClass="com.xx.cn.cxf_ws__helloworld.service.IUserService" address="http://localhost:9800/cxf_ws__spring/services/userService" ><jaxws:inInterceptors><bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/></jaxws:inInterceptors><jaxws:outInterceptors><bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" /></jaxws:outInterceptors></jaxws:client> </beans>package cxf_ws__spring.test;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.xx.cn.cxf_ws__helloworld.domain.User; import com.xx.cn.cxf_ws__helloworld.service.IUserService;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-test.xml") public class UserService_WS_Test {@Autowired@Qualifier("userServiceClient")private IUserService userService;@Testpublic void testCall() {System.out.println(userService.sayHello("瓊?cè)A派"));User user = new User();user.setUsername("xiaoming");System.out.println(userService.findCarsByUser(user));}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/god-1949-keshi/p/8283948.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的webService的简单使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入学习Java8 Lambda (de
- 下一篇: xfermode的基本用法