CXF整合Spring Web项目

一.服务端jar包 下载地址  (暂时没有,下面链接下载项目包里面自带

二.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>web</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
			classpath:applicationContext.xml
		</param-value>
  </context-param>
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class> 
            org.apache.cxf.transport.servlet.CXFServlet  
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

三.在src下面创建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.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
   

<!-- 注册bean address 是要访问的地址  比如 http://127.0.0.1:8080/server/HelloWorld?wsdl -->
<bean id="hello" class="test.HelloWorldImpl" /> 
    <jaxws:endpoint id="helloWorld" implementor="#hello" 
        address="/HelloWorld" />


</beans>

四.创建bean

package test;

import javax.jws.WebService;
@WebService 
//接口,虚加上注解
public interface HelloWorld {  
    //
	public String sayHello(String text); 

}

bean的实现类

package test;
import javax.jws.WebService;

@WebService(endpointInterface="test.HelloWorld")  

public class HelloWorldImpl implements HelloWorld {

	@Override
	public String sayHello(String text) {
		return text;
	}  

} 

然后我们放到tomcat下面启动之后访问  http://127.0.0.1:8088/server/HelloWorld?wsdl

我的tomcat端口号是8088,你们看自己的端口,然后server是项目名称,HelloWorld上面说过的,是接口的地址,访问webservices接口后面是必须加上wsdl的,访问之后可以看见一个xml文件就说明你的服务端创建完成了

 

二。创建客户端

方便起见,我们把服务端的jar包全部复制过来,放到客户端里面,其实客户端只需要spring的jar包就可以了

客户端可以创建成java项目

创建spring-client.xml

<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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
    <!-- class是指客户端的类路径 -->
    <bean id="client" class="com.text.HelloWorld"
      factory-bean="clientFactory" factory-method="create"/>

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
      <property name="serviceClass" value="com.text.HelloWorld"/>
      <property name="address" value="http://127.0.0.1:8088/server/HelloWorld?wsdl"/>
<!-- 这个地方的地址一定要注意,正确的-->
    </bean>

</beans>

二。创建客户端接口

package com.text;


import javax.jws.WebService;

@WebService
public interface HelloWorld { 

public String sayHello(String text); 

}

三.测试

package com.text;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.text.HelloWorld;

public class Test {  

    public static void main(String[] args) {  

        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                "spring-client.xml");  
        HelloWorld client = (HelloWorld) ctx.getBean("client");  
        String str = client.sayHello("你好!");  
        System.out.println(str);
    }  
} 

结果就会输出您好!

到这里webservices就结束了,请各位笑纳,

完整项目的下载地址链接:https://pan.baidu.com/s/1l-yMVhT_x1X9q8T5mQr_JA 
提取码:vvaq 

下载项目的小伙伴,我的tomcat端口是8088你们记得修改哦!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值