cxf整合springmvc
2015-12-01 17:03
337 查看
这个东西弄了一整天,网上参差不齐的例子搞的头都混乱的,结合了多个例子,处理了多个异常之后,终于成功了。
1.首先要倒入所需要的包
注意,这个包要直接复制到web-inf的lib目录下,不能通过java build path里add进来,不然会报找不到META-INF/cxf/cxf.xml的异常。
2.web.xml里增加配置
3.在src目录下增加个applicationContext.xml文件
4.写个接口
5.实现接口的类
6.启动服务器,然后写个测试类
控制台打印结果:
invoke webservice...
message context is:Hello gary, currentTime is Tue Dec 01 16:48:53 CST 2015
========================================分割线====================================================================
spring 客户端调用
1.在applicationContext.xml加入客户端的配置信息
2.controller里调用
控制台打印结果: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/")
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>
4.写个接口
package net.spring.webservice2; import javax.jws.WebService; @WebService public interface GreetingService { public String greeting(String userName); }
5.实现接口的类
package net.spring.webservice2; import java.util.Calendar; import javax.jws.WebService; @WebService(endpointInterface = "net.spring.webservice2.GreetingService") public class GreetingServiceImpl implements GreetingService { @Override public String greeting(String userName) { return "Hello " + userName + ", currentTime is " + Calendar.getInstance().getTime(); } }
6.启动服务器,然后写个测试类
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/")
相关文章推荐
- Struts1和Struts2的Action之线程安全
- Form表单数据传输方式之Get与Post区别
- JMeter学习——JMeter测试Java
- spring学习网站
- Java 语言中 Enum 类型的使用介绍
- JDK并发工具类源码学习系列——介绍
- URL编码解码原理及演示(Java演示)
- javax.xml.ws.soap.SOAPFaultException: Cannot create a secure XMLInputFactory
- org.springframework.orm.hibernate3.support.OpenSessionInViewFilter详解
- java中的权限问题
- java中IO方法详解
- Java打印菱形图案(3)
- HDU 1223 Order Count dp java
- springMVC入门实例
- 从头认识java-11.3 格式化输出(2)
- java 流操作
- struts2: config-browser-plugin 与 convention-plugin 学习
- Java获取中文拼音、中文首字母缩写和中文首字母
- JDK各版的发布时间表
- SpringMVC + poi 导出excel表格