CXF demo

来源:互联网 发布:thesaurus 知乎 编辑:程序博客网 时间:2024/05/29 19:04
=================服务端====================== 
实体类: 
package com.xxx.cxfserver.entity;public class User {private String name;private String password;public User() {super();}public User(String name, String password) {super();this.name = name;this.password = password;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User [name=" + name + ", password=" + password + "]";}}

服务接口: (ps: webservice不允许方法重载, 所以提供的服务中的方法名不能有重载的情况
package com.xxx.cxfserver.service;import java.util.List;import javax.jws.WebService;import com.xxx.cxfserver.entity.User;@WebServicepublic interface LoginService {Boolean login(String name, String password);User getUser(String name, String password);List<User> getUserList();String getUserInfo(User user);List<User> getUserLists(List<User> list);}

服务接口的实现: 
package com.xxx.cxfserver.service.impl;import java.util.ArrayList;import java.util.List;import javax.jws.WebService;import com.xxx.cxfserver.entity.User;import com.xxx.cxfserver.service.LoginService;@WebService(endpointInterface = "com.xxx.cxfserver.service.LoginService")public class LoginServiceImpl implements LoginService {@Overridepublic Boolean login(String name, String password) {Boolean result = false;if("admin".equals(name) && "123".equals(password)) {System.out.println("welcome to " + name);result = true;} else {System.out.println("Sorry, unrecognized username or password");}return result;}@Overridepublic User getUser(String name, String password) {return new User(name, password);}@Overridepublic List<User> getUserList() {List<User> list = new ArrayList<User>();for(int i = 0; i < 10; i++) {list.add(new User("test1" + i, "password" + i));}return list;}@Overridepublic String getUserInfo(User user) {if(user != null) {return "用户名是 : " + user.getName() + "  密码是 : " + user.getPassword();} else {return "用户对象是null";}}@Overridepublic List<User> getUserLists(List<User> list) {List<User> result = new ArrayList<User>();if(list != null && list.size() > 0) {for(int i = 0; i < list.size(); i++) {User user = list.get(i);user.setName("server" + i);user.setPassword("server_pwd" + i);result.add(user);}}return result;}}

服务器端bean文件的配置, 路径放在WEB-INF文件夹中, 配置如下: 
applicationContext.xml文件: 
<?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">
<import resource="server-servlet.xml"/></beans>
server-servlet.xml文件:
<?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-3.1.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:endpoint id="loginService" implementor="com.xxx.cxfserver.service.impl.LoginServiceImpl" address="/LoginService" /></beans>

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>CFXServer</display-name>    <!-- spring监听 -->  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>    <!-- cxf配置 -->  <servlet>  <servlet-name>CXFServices</servlet-name>  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  </servlet>    <servlet-mapping>  <servlet-name>CXFServices</servlet-name>  <url-pattern>/services/*</url-pattern>  </servlet-mapping></web-app>

=================服务端结束====================== 

=================客户端========================= 
客户端需要有和服务器端一样的服务类和实体类,如果服务器端有这些的话, 并且包名必须和服务器端的一样(这里指服务接口的包名,实体的可以不一样), 而且服务接口上必须有@WebService注解, 代码省略..................

客户端的bean文件的配置
<?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-3.1.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"><property name="serviceClass" value="com.xxx.cxfserver.service.LoginService" /><property name="address" value="http://localhost:8080/CFXServer/services/LoginService"></property></bean><bean id="client" class="com.xxx.cxfserver.service.LoginService" factory-bean="clientFactory" factory-method="create" /></beans>

测试类:
package com.xxx.cxfclient.test;import java.util.ArrayList;import java.util.List;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.util.Assert;import com.xxx.cxfserver.entity1.User;import com.xxx.cxfserver.service.LoginService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:cxf-client.xml")public class CXFClientTest {@Autowiredprivate LoginService ls;@Testpublic void test1() {Boolean result = ls.login("admin", "123");Assert.isTrue(result);}//..................省略剩下的测试代码}

用到的包:
服务端:


客户端: