我可以推荐CXF;我发现使用它的教程非常容易,特别是一旦我咬紧牙关并使用Maven来管理依赖关系(尽管它与CXF和Spring所做的所有事情都是正交的)。
但为了使用CXF,我真的推荐使用Spring。你不必使用Spring的全部; CXF网站上的简单教程使您足以开始学习。如果您已经获得实际已经完成的事情的代码,并从代码中分离出来以解析传入的URL,将响应呈现为XML等,那么尤其如此。这是CXF(通常是JAXB)将为您处理的部分。
为了帮助,这里有一个非常简单的例子(为简洁起见省略了导入)。 我知道它看起来很复杂,但几乎所有的后半部分都是由你写过的东西组成的,然后再不再触摸;当你构建出来处理你真实的代码时,你可以做很多事情,而不必付出必须关注框架代码。 首先,接口定义(包括XML类型的模型):
public interface Foo {
@Path("/") @GET @Produces("application/xml");
FooDesc getDescription(@Context UriInfo ui);
@Path("{id}")
FooItem getFoo(@PathParam("id") String id);
}
@Path("/")
public interface FooItem {
@GET @Produces("application/xml")
FooItemContents getContents();
@PUT @Consumes("application/xml")
void setContents(FooItemContents desc);
@DELETE
Response delete();
}
// These classes are purely structural holders; no behavior.
@XmlRootElement @XmlType
public class FooDesc {
@XmlElement
public List<URI> foo;
}
@XmlRootElement @XmlType
public class FooItemContents {
@XmlElement
String bar;
}
下,实现类:
public class FooImpl implements Foo {
public FooDesc getDescription(UriInfo ui) {
FooDesc desc = new FooDesc();
desc.foo = new ArrayList<URI>();
UriBuilder ub = ui.getAbsolutePathBuilder().path("{id}");
for (String id : realGetIdList()) // Hook to your code here!
desc.foo.add(ub.build(id));
return desc;
}
public FooItem getFoo(String id) {
final RealFoo r = realGetById(id); // Hook to your code here!
return new FooItem() {
public FooItemContents getContents() {
FooItemContents contents = new FooItemContents();
contents.bar = r.getBar(); // Hook to your code here!
return contents;
}
public void setContents(FooItemContents desc) {
r.setBar(desc.bar); // Hook to your code here!
}
public Response delete() {
r.close(); // Hook to your code here!
return Response.noContent().build(); // Return a simple HTTP 204
}
};
}
}
现在,水暖一起在春天水平与WEB-INF/beans.xml
:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<!-- Instantiate and connect the service framework -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="customerService" address="/">
<jaxrs:serviceBeans>
<ref bean="fooBean" />
</jaxrs:serviceBeans>
</jaxrs:server>
<!-- Instantiate your implementation class -->
<bean id="fooBean" class="your.package.FooImpl" />
</beans>
现在,把它作为一个web应用程序,一个web.xml
:
<web-app>
<!-- Magic to make Spring work and build the rest for us -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Make CXF implement the servlet for us -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- You *must* use this servlet mapping or Bad Things Happen. -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
现在建立包括在CXF网站上列出的所有库一个WAR,把位在正确的地方(你不需要Maven的这一点,但最终它可以更容易)和部署。这应该是有效的(即,你现在已经足够危险了!)
我已将您的代码添加到我使用cxf原型创建的Maven项目中。不幸的是,我收到了几十个错误,你能添加进口吗?我也想知道例如“RealFoo”在FooImpl.java的第15行,这个类从来没有定义。 – 2011-06-05 12:50:58