cxf webservice 整合spring 基本配置
发布时间:2020-12-17 00:58:20 所属栏目:安全 来源:网络整理
导读:1.使用的cxf版本2.6.0,tomcat6.0 2.必须引入的jar包 3.新建两个web工程 ?a.命名为cxftest ------ 服务端 ?b.命名为cilient--------客户端 先说服务端 修改web.xml ?加入代码 !-- spring begin-- listener listener-classorg.springframework.web.context.Co
1.使用的cxf版本2.6.0,tomcat6.0 2.必须引入的jar包 3.新建两个web工程 ?a.命名为cxftest ------> 服务端 ?b.命名为cilient-------->客户端 先说服务端 修改web.xml ?加入代码 <!-- spring begin--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-server.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- spring end --> <!-- cxf begin --> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <!-- cxf end --> 自己写个接口用于测试 package com.wjl; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface UserService { public UserBean getUser(@WebParam(name = "name") String name); public void setUser(UserBean user); }其中@webservice一定不能少 定义一个userbean public class UserBean { private int id; private String name; private String address; private String email;同时加入set get方法 下面完成接口的实现类 import java.util.Date; public class UserServiceImpl implements UserService { @Override public UserBean getUser(String name) { // TODO Auto-generated method stub UserBean user = new UserBean(); user.setId(new Date().getSeconds()); user.setName(name); user.setAddress("beijing"); user.setEmail(name + "@163.com"); return user; } @Override public void setUser(UserBean user) { // TODO Auto-generated method stub System.out.println("Server setUser!"); System.out.println("setUser:" + user); System.out.println("userName:" + user.getName()); System.out.println("userEmail:" + user.getEmail()); } } 下一步来完成spring配置文件 spring文件applicationContext,xml放在src目录下面即可 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://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-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <bean id="userServiceBean" class="com.wjl.UserServiceImpl"/> <jaxws:server id="userService" serviceClass="com.wjl.UserService" address="/Users"> <jaxws:serviceBean> <ref bean="userServiceBean"/> </jaxws:serviceBean> </jaxws:server> </beans> 注意: <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>一定要引入 现在启动tomcat ?访问:http://localhost:8080/cxftest/Users?wsdl 若出现以下则表示服务端配置成功 下面是client端 1.不使用spring 首先在client中新建一个接口,和服务端的一样即可 在创建一个测试类如下 package client.wjl;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class ClientClass {
/**
* @param args
*/
public static void main(String[] args) {
//调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(UserService.class);
factory.setAddress("http://localhost:8080/cxftest/Users");
UserService service = (UserService) factory.create();
System.out.println("#Client getUser");
UserBean user = service.getUser("wjl");
System.out.println(user);
user.setAddress("dota");
service.setUser(user);
}
}
执行即可 2.使用spring web,xml配置与服务端一样 spring配置为如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://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-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <bean id="client" class="client.wjl.UserService" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="client.wjl.UserService"/> <property name="address" value="http://localhost:8080/cxftest/Users"/> </bean> </beans> 在创建测试类 package client.wjl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class clientSpring { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext-server.xml"); UserService service = (UserService) ctx.getBean("client",UserService.class); UserBean user = service.getUser("reven"); System.out.println(user.getAddress()); System.out.println(user.getEmail()); user.setAddress("China"); service.setUser(user); } }执行即可 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |