cxf 配置文件服务器端,cxf整合spring实现webservice

前面一篇文章中,webservice的服务端与客户端都是单独启动,但是在现实项目中,服务端单独启动太没有实际意义了,还是要整合框架启动,所以今天将记录如何整合spring框架。

jar包下载地址如下:

(一)、web.xml中添加spring与cxf的配置<?xml  version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

CXFServlet

org.apache.cxf.transport.servlet.CXFServlet

1

CXFServlet

/*

index.jsp

(二)、新建接口文件,实现类

接口,需要添加注解webservice@WebService

public interface IHello {

public String sayHi(String name);

public String printName(String name);

}

实现接口类,指定endpointInterface与serviceName@WebService(endpointInterface="com.xj.service.IHello",serviceName="hello1Service")

public class HelloImpl implements IHello{

@Override

public String sayHi(String name) {

System.out.println("hi,"+name);

return "hi,"+name;

}

@Override

public String printName(String name) {

System.out.println("my name is,"+name);

return "my name is,"+name;

}

}

(三)、配置applicationContext.xml配置文件<?xml  version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:jaxws="http://cxf.apache.org/jaxws"

xmlns:cxf="http://cxf.apache.org/core"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://cxf.apache.org/jaxws

http://cxf.apache.org/schemas/jaxws.xsd">

需要在文件中引入这三个文件,加入相关约束

解释:

最关键的是jaxws:endpoint标签,这与直接启动server时,endpoint的publish作用相同,发布这个服务,implementor指定了实现类,这里用的是引用的方式,可以直接如下

(四)、项目部署并访问

将项目部署到tomcat中,启动无报错

访问   http://localhost/cxf/testHello?wsdl 可得到我们发布服务的wsdl,其中cxf为项目名称,testHello为我们endpoint中配置的路径

到此为止,服务端就发布完成了。

一般项目中大部分都是单独提供server端接口(即发布服务的wsdl地址),或者根据wsdl地址生成client端。

附加

(五)、启动client端

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:jaxws="http://cxf.apache.org/jaxws"

xmlns:cxf="http://cxf.apache.org/core"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://cxf.apache.org/jaxws

http://cxf.apache.org/schemas/jaxws.xsd">

解释:

jaxws:client 标志为服务的client端,其中address指定了需要访问的wsdl地址,id指定了一个唯一的bean标志。public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextClient.xml");

IHello hello = (IHello) ctx.getBean("hello");

System.out.println(hello.sayHi("xiejun"));

}

如上述,就可以调用服务的相关方法,但是这种调用方法需要项目中含有server端的基础类,如IHello等,显然不太现实。下一篇文章中讲继续记录 如何调用天气预报的接口,现在在那我们是没法知道server端的基础类的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值