cxf2.2.6整合spring3.2实现Client端和Server端2.

一、坑

1.springmvc的容器是DispatcherServlet,webservice的容器是CXFServlet。

   在webservice中用@Resource和@Autowired加载springmvc的bean都不生效,即注入会失败

2.既然是两个容器,webservice是拿不到springmvc的bean的,那么解决方法之一:

   spring的扫描器 <context:component-scan base-package="com.bethere.ams.wservice.**" />

       我们可以使用@Resource(亲测有效)和@Autowired(未测试),然后再配置下spring的扫描器,使我们写的注解生效即可实现bean的自动载入

   当然还会有别的方法,请参见 此篇文章(未测试),或自己查询别的解决办法。

3.@Resource和@Autowired区别(请自行学习),或可参见此篇文章

二、具体重要配置及代码(项目没用maven):

1.cxf2.2.6必须的jar包请参见上一篇文章

2.web.xml配置webservice配置文件及核心Servlet

 <!-- 配置webservice配置文件,我的叫spring-cxf.xml-->

            <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/application-context.xml,
			/WEB-INF/spring-cxf.xml
		</param-value>
	    </context-param>

 <!-- 配置CXF框架的核心Servlet -->
  

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

3.spring-cxf.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	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   
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<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" />

	<!-- 对外提供的WebService接口访问地址 -->
	<!-- 下面这个示例服务访问地址就是http://localhost:8080/ams/ws/mineService?wsdl,为什么前面要加一个ws,请看web.xml里面的cxfService配置 -->
	<!-- webService一定要有接口和实现类,否则会出错 -->
	<bean id="mineServiceImpl" class="com.bethere.ams.wservice.impl.MineServiceImpl" />
	<jaxws:server id="mineService" serviceClass="com.bethere.ams.wservice.MineService"
		address="/mineService">
		<jaxws:serviceBean>
			<ref bean="mineServiceImpl" />
		</jaxws:serviceBean>
	</jaxws:server>
	
	<!--注解扫描-->
	<context:component-scan base-package="com.bethere.ams.wservice.**" />
	

</beans>

4.接口及实现

package com.bethere.ams.wservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * webService
 * 
 * @author zx
 * 
 */
@WebService
public interface MineService {
	/**
	 * Demo
	 * 
	 * @param appUserId
	 * @param title
	 * @param questionContent
	 * @param source
	 * @return
	 */
	@WebMethod
	public String sendQuestion(@WebParam(name = "appUserId") String appUserId, @WebParam(name = "title") String title, @WebParam(name = "questionContent") String questionContent);

}

实现:

package com.bethere.ams.wservice.impl;

import javax.annotation.Resource;
import javax.jws.WebService;

import com.alibaba.fastjson.JSON;
import com.bethere.ams.domain.AmsEquip;
import com.bethere.ams.util.ResultJson;
import com.bethere.ams.wservice.MineService;
import com.bethere.pms.sysmng.service.UserService;
@WebService(endpointInterface = "com.bethere.ams.wservice.MineService",targetNamespace="http://wservice.ams.bethere.com/")
public class MineServiceImpl implements MineService {
	@Resource
	private UserService userService;

	@Override
	public String sendQuestion(String appUserId, String title, String questionContent) {
		// 进行业务处理
		AmsEquip equip = userService.get(AmsEquip.class, "0007E1CA9F4D482B8E192DCC036D8D82");
		System.out.println("equip OK!");
		// 返回值封装
		ResultJson json = new ResultJson();
		// 异常系的errorCode
		// json.setCode(CodeMsgContant.USER_OPENID_IS_NULL);
		json.setMsg("返回的msg");
		json.setSuccess(true);
		json.setObj("返回的obj");
		return JSON.toJSONString(json);
	}
}

5.访问接口的工具类

package com.bethere.ams.util;

import java.net.URL;

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

public class WsUtil {
	static JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
	static URL url;
	/**
	 * 访问接口的方法
	 * @param methodName 你要访问的接口名称
	 * @return obj 你要传给接口的参数
	 * */
	public static String inviteIF(String methodName, Object... obj) {
		try {
			url= new URL("你的wsdl成功的地址,如http://localhost:8080/ams/ws/mineService?wsdl");
			Client client = dcf.createClient(url);
			System.out.println("ws_url:"+url);
			Object[] objects = client.invoke(methodName, obj);
			return objects[0].toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

如有错误的地方请指教。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值