CXF rest客户端不使用JAXRSServerFactoryBean

时间:2013-10-15 08:08:59

标签: java rest client cxf

我想使用CXF创建一个rest客户端。以下代码抛出异常

JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
        sf.setResourceClasses(Blogger.class);
        sf.getInInterceptors().add(new LoggingInInterceptor());
        sf.getOutInterceptors().add(new LoggingOutInterceptor());
        sf.setAddress("http://myservice");
        Map<Object, Object> mappings = new HashMap<Object, Object>();
        mappings.put("xml", "application/xml");
        mappings.put("json", "application/json");
        sf.setExtensionMappings(mappings);
        sf.create();

我得到的例外是

Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/WebApplicationException
    at com.test.LoggingTest.main(LoggingTest.java:26)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.WebApplicationException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 1 more

2 个答案:

答案 0 :(得分:4)

类路径中缺少javax.ws.rs API .jar。更正依赖关系管理工具的设置或手动将其添加到类路径。来自Apache CXF documentation

  • 2.7需要JSR-339 api(JAX-RS 2.0):javax.ws.rs/javax.ws.rs-api/m10
  • 2.6及更早版本使用JSR-311 api(JAX-RS 1.0)

答案 1 :(得分:3)

您是否已将jsr311-api-1.1.jar包含在您的类路径中?如果您使用maven,这可能对您有帮助(JAS-RS 1.1)

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>jsr311-api</artifactId>
    <version>1.1.1</version>
</dependency>

或JAX-RS 2.0版

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
</dependency>