环境:Jdk版本1.6.0_03
CXF版本2.6.16
说明:有很多错误是由于少了某些jar引起的,但是eclipse控制台可能不会报java.lang.NoClassDefFoundError错误,所以解决这样的问题比较麻烦。
problem:java.lang.RuntimeException: Cannot create a secure XMLInputFactory 运行时异常,并不是一定是缺少两个jar包 stax2-api-3.1.x.jar 和 woodstox-core-asl-4.2.x.jar而引起的异常,缺少其他jar包也可能出现这个异常。
开发项目的目录结构以及jar,如图
开发步骤:
1.接口login.java
package cxf.service; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface Login { public String login(@WebParam(name="name")String name,@WebParam(name="password")String password); }
2.实现login接口
package cxf.service; import javax.jws.WebService; @WebService(endpointInterface="cxf.service.Login",serviceName="Login") public class LoginImpl implements Login { public String login(String name, String password) { String result = "登录CXF 服务端成功!"; if (!"cxf".equalsIgnoreCase(name) || !"cxf".equalsIgnoreCase(password)) { result = "用户名或密码不正确,请重新登录!"; } return result; } }
3.编写服务端(启动服务)
package cxf.service; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class Pub { public static void main(String[] args) { LoginImpl loginImpl = new LoginImpl(); JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean(); bean.setServiceClass(Login.class); bean.setServiceBean(loginImpl); bean.setAddress("http://localhost:9000/helloWorld"); bean.create(); System.out.println("Finish...."); } }
4.编写客户端(测试类)
package cxf.client; /** * CXF2.6.16依赖的jar包 */ import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import cxf.service.Login; public class Client { public static void main(String[] args) { JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean(); bean.setServiceClass(Login.class); bean.setAddress("http://localhost:9000/helloWorld"); Login login = (Login) bean.create(); System.out.println("result--->"+login.login("cxf", "cxf")); } }
5.经过如上的4个步骤之后还是不行,因为CXF与jdk中的类不兼容,解决办法如下:
CXF中jaxb-api.jar、jaxws-api.jar与jdk1.6.0_02不兼容问题
http://villain564.iteye.com/blog/963967
参考:http://villain564.iteye.com/blog/963967
参考:http://www.cnblogs.com/frankliiu-java/articles/1641949.html