想往架构师方向发展,他们说soa必须学会WEB SERVICE。以下简称ws.
鄙人用myeclipse 6.5
1、服务端:
创建web项目。
右键myeclipse add web service ......
增加了一些jar包进来,主要是xfire和其他的东西
会让我们自己设置web service配置文件的路径,还有web.xml里面配置ws的url。
web.xml生成了这个摸样:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/testyqf/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
自己定义一个接口:
package com.yuan.ws; import com.yuan.pojo.User; public interface WebService { public User login(User user); }
弄一个简单的实现类:
package com.yuan.ws.impl; import com.yuan.pojo.User; import com.yuan.ws.WebService; public class WebServiceImpl implements WebService { public User login(User user) { if(user.getUserName().equals("yqf")){ return new User("1","yqfright","1234"); }else { return new User("1","yqfww","12344"); } } }
我的pojo类:
package com.yuan.pojo; public class User { private String userId; private String userName; private String userPwd; public User() { super(); // TODO Auto-generated constructor stub } public User(String userId, String userName, String userPwd) { super(); this.userId = userId; this.userName = userName; this.userPwd = userPwd; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } }
这个时候,你必须自己配置ws了,接口以及实现类:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>login</name> <serviceClass>com.yuan.ws.WebService</serviceClass> <implementationClass>com.yuan.ws.impl.WebServiceImpl</implementationClass> <style>wrapped</style> <use>literal</use> <scope>application</scope> </service> </beans>
发布tomcat启动 哈哈
浏览器访问地址:http://127.0.0.1:8090/TestServer/testyqf/
会显示可用的ws类型,选择一个就是ws的wsdl。什么是wsdl?就是描述我们提供对外功能的一个xml文档,一般高手可用看文档然后在客户端编写,我现在还不会,看那个玩意受罪。
2、客户端:
随便写一个类:
import java.net.MalformedURLException; import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.yuan.pojo.User; import com.yuan.ws.WebService; public class Test { /*public static void main(String[] args) { Service srvcModel = new ObjectServiceFactory().create(IHelloWorldService.class); XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); String helloWorldURL ="http://127.0.0.1:8090/WebService/services/HelloWorldService"; try{ IHelloWorldService srvc = (IHelloWorldService)factory.create(srvcModel, helloWorldURL); String result = srvc.example("hello world"); System.out.print(result); } catch (MalformedURLException e){ e.printStackTrace(); } } */ public static void main(String[] args) { Service srvcModel = new ObjectServiceFactory().create(WebService.class); XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); String helloWorldURL ="http://127.0.0.1:8090/TestServer/testyqf/login"; try{ WebService srvc = (WebService)factory.create(srvcModel, helloWorldURL); User retUser = srvc.login(new User("1","yqf","123")); System.out.print(retUser.getUserName()); } catch (MalformedURLException e){ e.printStackTrace(); } } }
这里需要把服务器的接口拷贝过来,补考呗也可用,build path把服务器的project 引入进来。
运行,就出来结果啦。