一、坑
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;
}
}
如有错误的地方请指教。