CXFSpring3例子

写一下关于CXF的例子,网上也有一些,综合了一下,以备忘首先是jar包的问题,这个问题我纠结了好几天,首先是jax等包的冲突问题,这个问题很多人都有解释,比如换1.6以上的jdk,或者通过

System.out.println(System.getProperty(“java.endorsed.dirs”)); 

 

其实就是得到对应的jdk路径,然后将jax 和jaxapi等冲突包扔进去。

不过在我开发过程中遇到无数次的提示,弄的我也挺郁闷,后来还是将项目路径下的jdk干掉才跑通,想了想估计是项目路径下的jdk优先权高?应该的吧。

以下是服务器端代码:

1、首先是 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	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">
	
	<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>
	<listener>
		<listener-class>
		org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<servlet>
		 <display-name>CXF Servlet</display-name>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- CXFServlet Mapping -->
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 简单说就是配置了一个servlet去拦截请求

2、Spring 配置文件 application.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:p="http://www.springframework.org/schema/p"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://cxf.apache.org/jaxws 
     http://cxf.apache.org/schemas/jaxws.xsd">
<!--  命名空间-->


 <!-- Import Apache CXF Bean Definition -->
    <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="iSuimp" class="com.ws.cxf.imp.ISuimp">
		<property name="temp1" value="111"></property>
		<property name="temp2" value="444"></property>
</bean>

<!--2个参数无实际意义,就是为了测试注入用的 -->
	<jaxws:server id="iSuInterface" serviceClass="com.ws.cxf.ISuInterface" address="/ISuService">
		<jaxws:serviceBean>
			<ref bean="iSuimp"/>
		</jaxws:serviceBean>
	
	</jaxws:server>
<!-- 一个webservice选项,地址是xxxx/ISuService,因为是注入自然class为一个接口  wsdl不解释如何写-->
</beans>

 3、接口类

package com.ws.cxf;

import javax.jws.WebService;

@WebService
public interface ISuInterface {
	public String vote(String username);
}

 

就是写了一个注解而已

4、实现类

package com.ws.cxf.imp;

import javax.jws.WebService;

import com.ws.cxf.ISuInterface;
@WebService
public class ISuimp implements ISuInterface{
	private  String temp1;
	private  int  temp2;

	
	public String getTemp1() {
		return temp1;
	}


	public void setTemp1(String temp1) {
		this.temp1 = temp1;
	}


	public int getTemp2() {
		return temp2;
	}


	public void setTemp2(int temp2) {
		this.temp2 = temp2;
	}


	public String vote(String username) {
		return username.equals("1")?"相同":"不同";
	}

}

 

写变量的意思是为了测试能否注入如其他业务类才加入的。

以上为服务器端代码,重点在于application中的配置,引入的一些schema.

 

以下为客户端的代码:

为了提高通用性,毕竟我们不能保证每一家service提供jar包,见到网上很多都是用强制类型转换得到service对象的,觉得不太合适,所以还是采用了其他的方式(原理我还不甚理解),当然服务端与客户端在实际应用中应该属于不同项目,因此,还是需要将相应的jar包导入到客户端项目中,我当时想使用spring mvc来做一个客户端,可惜没弄成,总是jar包冲突,readwsdl时总是出错,看得出来也是版本冲突,不过那个我比较难解决,于是采用了servlet的方式来做。

web.xml只是一个servlet的拦截器(客户端xml应该与cxf无关,无需导入对应的schema)

 以下为客户端代码

package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class CxfServletClient extends HttpServlet{
	private static final long serialVersionUID = 1L;
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		 JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance(); 

	        Client client = clientFactory.createClient("http://localhost:8000/CXF_Spring/ISuService?wsdl"); 

	        Object[] result;
			try {
				result = client.invoke("vote","1");
				 System.out.println(result[0]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	       
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}

 值得注意的是createClient("http://localhost:8000/CXF_Spring/ISuService?wsdl"); 这里要写的url为wsdl。。。可以用浏览器先测试一下能否出现wsdl再进行配置。

 

 

注:cxf也是使用soap协议,只能传递简单对象~~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值