cxf整合springmvc
来源:互联网 发布:js弹出div模态窗口 编辑:程序博客网 时间:2024/06/05 07:21
这个东西弄了一整天,网上参差不齐的例子搞的头都混乱的,结合了多个例子,处理了多个异常之后,终于成功了。
1.首先要倒入所需要的包
cxf-2.7.8.jar neethi-3.0.2.jar xmlschema-core-2.0.3.jar wsdl4j-1.6.3.jar asm-3.3.jar stax2-api-3.1.x.jar woodstox-core-asl-4.2.x.jar
注意,这个包要直接复制到web-inf的lib目录下,不能通过java build path里add进来,不然会报找不到META-INF/cxf/cxf.xml的异常。
2.web.xml里增加配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
3.在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"><import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:endpoint id="greetingService1111"implementorClass="net.spring.webservice2.GreetingServiceImpl" address="/wsPostName1111" /></beans>
package net.spring.webservice2;import javax.jws.WebService;@WebServicepublic interface GreetingService {public String greeting(String userName);}
package net.spring.webservice2;import java.util.Calendar;import javax.jws.WebService;@WebService(endpointInterface = "net.spring.webservice2.GreetingService")public class GreetingServiceImpl implements GreetingService {@Overridepublic String greeting(String userName) {return "Hello " + userName + ", currentTime is "+ Calendar.getInstance().getTime();}}
package net.spring.webservice2;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class TestWs {/** * @param args */public static void main(String[] args) {// 创建WebService客户端代理工厂JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();// 注册WebService接口factory.setServiceClass(GreetingService.class);// 设置WebService地址factory.setAddress("http://localhost:8080/springtest/services/wsPostName1111");GreetingService greetingService = (GreetingService) factory.create();System.out.println("invoke webservice...");System.out.println("message context is:" + greetingService.greeting("gary"));}}
控制台打印结果:
invoke webservice...
message context is:Hello gary, currentTime is Tue Dec 01 16:48:53 CST 2015
========================================分割线====================================================================
spring 客户端调用
1.在applicationContext.xml加入客户端的配置信息
<jaxws:client id="testServiceClient" serviceClass="net.spring.webservice2.GreetingService"address="http://localhost:8080/springtest/services/wsPostName1111"></jaxws:client>
2.controller里调用
@RequestMapping(value = "webservice", method = RequestMethod.GET)public void webservice(HttpServletRequest request){//// 创建WebService客户端代理工厂//JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();//// 注册WebService接口//factory.setServiceClass(GreetingService.class);//// 设置WebService地址//factory.setAddress("http://localhost:8080/springtest/services/wsPostName1111");//GreetingService greetingService = (GreetingService) factory.create();//System.out.println("invoke webservice...");//System.out.println("message context is:" + greetingService.greeting("gary"));ServletContext servletContext = request.getSession().getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); GreetingService service = (GreetingService)ctx.getBean("testServiceClient");System.out.println("message context is:" + service.greeting("gary"));}
控制台打印结果:message context is:Hello gary, currentTime is Wed Dec 09 09:53:33 CST 2015
======================================================================
客户端调用如果出现以下异常的话
org.apache.cxf.binding.soap.SoapFault: Unexpected wrapper element
是因为服务端的接口和客户端的接口的包名不一样,所以要在客户端的接口上加上一句话
如果服务端的包名是com.test111.server,客户端的报名是com.test222.server,那么客户端的接口类上要这样写
@WebService(targetNamespace = "http://server.test111.com/")
0 0
- cxf整合springmvc
- springmvc 和 cxf整合
- SpringMVC整合CXF开发WebService
- springmvc整合cxf遇到的坑
- CXF webService 与 SpringMVC 整合要点
- 一、CXF与springMVC整合的webService发布
- 二、CXF与springMVC整合的webService客户端调用
- 五、CXF整合Spring
- CXF整合Spring
- CXF WebService整合Spring
- Spring 整合 CXF
- CXF整合Spring入门教程
- CXF WebService整合Spring
- CXF WebService整合Spring
- cxf和spring整合
- spring+cxf+bonecp整合
- CXF WebService整合Spring
- CXF Spring整合案例
- ELaticSearch——PlainElastic.net.dll 中的JsonNetSerializer
- URL编码解码原理及演示(Java演示)
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- 网络层—网际协议IP
- hive表导出
- cxf整合springmvc
- Acivity的启动模式
- 【C#】Timer、窗体大小和屏幕大小
- HBase Metrics
- 网络设备驱动程序数据结构
- Android 4.0新控件 switch的属性
- 支持向量机通俗导论(理解SVM的三层境界)
- Tomcat在window下直接启动出现一闪而过
- JavaScript 的类型检测