使用apache cxf的RESTful Web服务

时间:2015-06-18 09:57:17

标签: web-services rest soap cxf

我已经使用apache cxf用Java开发了一个SOAP Web服务,现在我已经在RESTful中开发了相同的服务。

我在tomcat上部署Web服务时遇到了一些麻烦。事实上,我不知道应该如何配置web.xml或任何其他XML文件。

网上有很多例子,但我到目前为止所做的一切都不起作用。

要构建项目,我使用Ant构建工具。

有人知道一些好的建议吗?

1 个答案:

答案 0 :(得分:1)

这里我的服务类用于测试教程

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/book")
public class BookService {

@GET
@Path("{id}")
@Produces({"application/xml","application/json"})
public Book getBookForId(@PathParam("id") int id) {
    Book book = null
    book = DB.getBookForId();
    if(book == null){
        return Response.status(Response.Status.BAD_REQUEST).build();
    }else{
        return Response.ok(book).build();
    }
}

}

我的web.xml大部分时间都是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
    <display-name>REST</display-name>


<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>

Apache CXF的示例和其他示例“易于理解”,但快速的是我到目前为止所做的事情无法正常工作。

我的网络服务部署在我的tomcat上,但每次都有这样的错误:

 Servlet.service() for servlet [Jersey Web Application] in context with path [/WebServices] threw exception [L''exécution de la servlet a lancé une exception] with root cause
java.lang.AbstractMethodError:     javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

我知道这种错误是愚蠢的btu我没有得到Zzz。

我必须使用jdk 1.6.0.45所以我不能尝试使用jdk 1.7及更高版本的示例。事实上,大多数例子告诉我们使用泽西岛2.18 ......

相关问题