开发工具:myeclipse 6.5
cxf 版本:2.3.3
server:
package com.cxf.server;
public interface CxfService {
String sayHello(String username);
}
package com.cxf.server;
public class HelloWorldCxfServiceImpl implements CxfService {
public String sayHello(String username) {
return "Hello,"+username;
}
}
package com.cxf.server;
import org.apache.cxf.frontend.ServerFactoryBean;
public class Server {
public static void main(String[] args){
HelloWorldCxfServiceImpl worldCxfServiceImpl=new HelloWorldCxfServiceImpl();
ServerFactoryBean factoryBean=new ServerFactoryBean();
factoryBean.setAddress("http://localhost:8080/helloword");
factoryBean.setServiceClass(CxfService.class);
factoryBean.setServiceBean(worldCxfServiceImpl);
factoryBean.create();
}
}
client:
package com.cxf.client;
public interface CxfService {
String sayHello(String username);
}
package com.cxf.client;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import com.cxf.server.CxfService;
public class Client {
public static void main(String[] args) {
ClientProxyFactoryBean factoryBean=new ClientProxyFactoryBean();
factoryBean.setAddress("http://localhost:8080/helloword");
factoryBean.setServiceClass(CxfService.class);
CxfService worldCxfService=(CxfService) factoryBean.create();
System.out.println(worldCxfService.sayHello("张三"));
}
}
低版本jdk6与cxf中的jaxb-api.jar、jaxws-api.jar会存在不兼容问题
具体异常:
java.lang.NoClassDefFoundError: javax/xml/ws/soap/MTOM–>jaxws-api.jar
java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSeeAlso–>jaxb-api.jar
因为cxf2.3.3需要jaxws-api-2.1.jar及jaxb-api-2.1.jar支持,而jdk1.6.0_02中默认的是jaxws-api-2.0.jar及jaxb-api-2.0.jar
要解决这个问题有两种处理方式:
1、升级jdk来解决,我升级到jdk1.6.0_22及以后版本是可以使用的
2、让低版本jdk也能支持jaxws-api-2.1.jar及jaxb-api-2.1.jar,那么可以如下处理:
写一个main函数,输出System.out.println(System.getProperty(“java.endorsed.dirs”)); 获得endorsed目录的位置,如果没有endorsed目录则新建,并宝贝jaxws-api-2.1.jar和jaxb-api-2.1.jar到endorsed目录下,那么jdk1.6.0_02调用的jaxws-api.jar及jaxb-api.jar也全是2.1的