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使用说明

参数说明-p定义客户端生成类的包名称-s指定客户端执行类的源文件存放目录-d指定客户端执行类的class文件的存放目录-keep表示生成客户端执行类的源代码-b指定jaxws/jaxb绑定文件或额外的schemas-extension使用扩展来支持SOAP1.2

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
原创粉丝点击