WebService Spring4.3+cxf3.1.5整合Demo
来源:互联网 发布:辽宁省软件协会官网 编辑:程序博客网 时间:2024/05/21 18:39
1、准备好cxf的jar包,maven配置
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.5</version> </dependency>
以及web.xml的配置,以拦截wbService请求
<!--webService --> <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>/webservice/*</url-pattern> </servlet-mapping>
2、开始写webservice服务接口;
import com.redBlue.slj.model.User;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;/** * Created by yangf on 2016/11/12. */@WebService@SOAPBinding(style = SOAPBinding.Style.RPC)public interface SayCXFService { @WebMethod public String sayHello(@WebParam(name = "name") String name); @WebMethod public User getUser();}
接口的实现类:
import com.redBlue.slj.dao.UserDao;import com.redBlue.slj.model.User;import com.redBlue.slj.webService.SayCXFService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * Created by yangf on 2016/11/12. */@Service("sayCXFService")public class SayCXFServiceImpl implements SayCXFService{ @Autowired private UserDao userDao; @Override public String sayHello(String name) { System.out.println("我被调用了——————"); return name +" hello,this is my first CXF webService!"; } @Override public User getUser() { User user = userDao.selectByPrimaryKey(1L); user.setEmail("12312312"); return user; }}
3、服务接口有了,那么当然需要发布到webService服务器上了
- 在spring配置文件中引入下面配置文件
<!--引入webService 发布配置--> <import resource="spring-webService.xml" />
- 新建配置文件spring-webService.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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- cxf3以后,只需要引入这个配置文件即可,其他两个废弃掉了--> <import resource="classpath:META-INF/cxf/cxf.xml" /> <!-- implementor 表示你要对外开发的接口实现类,address表示的地址,本质就是一个servletName,可以随便命名 --> <jaxws:endpoint id="webService" implementor="com.redBlue.slj.webService.impl.SayCXFServiceImpl" address="/sayCXFServiceWs"> </jaxws:endpoint> <jaxws:endpoint id="userService" implementor="com.redBlue.slj.service.impl.UserServiceImpl" address="/userService"> </jaxws:endpoint></beans>
发布服务的地址http://127.0.0.1/webservice/sayCXFServiceWs?wsdl 通过wsdl能解析出接口信息
客户端
当然客户端也需要spring管理和webservice,所以引入下面三个包
<!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- cxf,webService用 Start --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.5</version> </dependency> <!-- cxf,webService用 end -->
cmd命令使用java中的工具可以生成客户端的接口
wsimport -keep http://127.0.0.1/webservice/userService?wsdl
wsimport使用说明
1、客户端调用代码
import com.redblue.slj.webservice.User;import com.redblue.slj.webservice.impl.SayCXFService;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class ClientTest { public static void main( String[] args ){// String classPath = ClientTest.class.getResource("/").getPath();// System.out.println(classPath); ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext( new String[] {"client-bean.xml" }); SayCXFService sayCXFService = (SayCXFService) classPathXmlApplicationContext .getBean("sayCXFService"); User user = sayCXFService.getUser(); System.out.println("-------------------" + user); System.out.println( "Hello World!" ); }}
客户端的配置文件,client-bean.xml如下
<?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"> <jaxws:client id="sayCXFService" serviceClass="com.redblue.slj.webservice.impl.SayCXFService" address="http://127.0.0.1/webservice/sayCXFServiceWs"> </jaxws:client> <jaxws:client id="userService" serviceClass="com.redblue.slj.service.impl.IUserService" address="http://127.0.0.1/webservice/userService"> </jaxws:client></beans>
0 0
- WebService Spring4.3+cxf3.1.5整合Demo
- cxf3.1.4整合spring4.1.7 demo-xs
- CXF3.1.6+Spring4.1.9整合
- 利用cxf3.14+spring4.2发布webservice
- 使用 Spring4 + CXF3 + WS-Security 开发 WebService
- 简单CXF3.0整合Spring4.1
- spring4.x + cxf3.x整合实例
- Spring整合CXF3.1实现webservice
- cxf3.14+spring4.2以rest风格发布webservice
- 手把手教你 cxf3.1.4整合spring4.2
- spring4.1.6+cxf3.0.8的简单WebService案例(maven工程)
- maven3.1.1 spring4.3.4+mybatis3.2.8+CXF3.1.7 发布webservice 注意几点
- [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口
- 转载 [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口
- cxf3.0+spring4.0.5错误
- webservice-CXF3.0
- spring4+mybaits3整合—项目Demo
- spring4+mybaits3整合—项目Demo
- 子集生成算法 之 位向量法和增量构造法
- 413. Arithmetic Slices 【M】
- dwz框架dialog提交并刷新列表
- ue4学习记录-1-旋转门
- 【搜索】【NOIP2004】虫食算
- WebService Spring4.3+cxf3.1.5整合Demo
- 啾啾拼音-小学“看拼音写词语”工具
- 常用yum源收集
- KICKSTART测试
- 文章标题 POJ 1258 : Agri-Net(最小生成树--kruskal)
- 事务
- Android Studio 中手把手教你设置switch/case代码块自动补齐
- 原子性、内存可见性和重排序——重新认识synchronized和volatile
- 软考(五)-考后总结