【webservice】JAX-WS独立服务使用
生活随笔
收集整理的這篇文章主要介紹了
【webservice】JAX-WS独立服务使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【webservice】JAX-WS獨(dú)立服務(wù)使用
建立maven Java項(xiàng)目,在項(xiàng)目中導(dǎo)入CXF jar包的支持,要提供jaxws服務(wù),就引入jaxws的jar包,要提供jaxrs服務(wù),就要引入jaxrs的jar包。
使用maven坐標(biāo)
<!--要進(jìn)行jaxws服務(wù)開發(fā)--> <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version> <dependency> <!--內(nèi)置jetty web服務(wù)器--> <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.0.1</version> </dependency>查看完整日志
<!--日志實(shí)現(xiàn)--> <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.12</version> </dependency>要使用日志,同時(shí)需要導(dǎo)入log4j.properties配置文件.
編寫服務(wù)端程序。編寫實(shí)體類
public class User{private Integer id;private String username;private Sting city;private List<Car> cars = new ArrayList<Car>(); } public class Car{private Integer id;private String carName;private Double price; }編寫服務(wù)
@WebService public interface IUserService {@WebMethodpublic String sayHello(String name);@WebMethodpublic List<Car> findCarsByUser(User user); }@WebService使用在類上面,標(biāo)記類是WebService服務(wù)提供對(duì)象;
@WebMethod使用在方法上面,標(biāo)記方法是WebService服務(wù)的提供方法。
服務(wù)實(shí)現(xiàn)
@WebService(endpointInterface = "cn.nwtxxb.cxf.services.IUserService",serviceName="userService") public class UserServiceImpl implements IUserService {//簡單參數(shù)傳遞public String sayHello(String name){return "hello,"+name;}//復(fù)雜參數(shù)傳遞public List<Car> findCarsByUser(User user){if("xiaoming").equals(user.getUsername()){List<Car> cars = new ArrayList<Car>();Car car1 = new Car();car1.setId(1);car1.setName("凱迪拉克");car1.setPrice(200000d);cars.add(car1);}} }@WebService注解設(shè)置endPointInterface接口服務(wù)完整類名,servicename是服務(wù)名稱。
想將UserService的服務(wù)方法發(fā)布到網(wǎng)絡(luò)上,供其他系統(tǒng)調(diào)用。
public static void main(String[] args){//使用CXF將UserService服務(wù)注冊(cè)到網(wǎng)絡(luò)上//1.服務(wù)的實(shí)現(xiàn)對(duì)象IUserService userService = new UserServiceImpl();//2.發(fā)布服務(wù)的地址String address = "http://localhost:9999/userService";//3.發(fā)布服務(wù)的地址Endpoint.publish(address,userService);System.out.println("服務(wù)已經(jīng)啟動(dòng)..."); }訪問網(wǎng)址:http://localhost:9999/userService?wsdl
編寫客戶端操作
public static void main(String[] args){//編寫客戶端 調(diào)用發(fā)布WebService服務(wù)JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxProxyFactoryBean();jaxWsProxyFactoryBean.setServiceClass(IUserService.class);jaxWsproxyFactoryBean.setAddress("http://localhost:9999/userService");//創(chuàng)建調(diào)用遠(yuǎn)程服務(wù)代理對(duì)象IUserService proxy = (IUserService)jaxWsProxyFactoryBean.create();//調(diào)用代理對(duì)象的任何一個(gè)方法,都將通過網(wǎng)絡(luò)調(diào)用web服務(wù)System.out.println(proxy.sayHello("你我他學(xué)習(xí)吧"));總結(jié)
以上是生活随笔為你收集整理的【webservice】JAX-WS独立服务使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【软考】2017年11月软件设计师上午真
- 下一篇: 【软考-系统架构师】计算机操作系统相关考