java Spring Boot 与 Apache CXF 的 RESTful Web 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25083045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot vs. Apache CXF for RESTful Web Services?
提问by user3899879
I am part of a coding competition, the task is to create a RESTful online marketplace where users can post buy and sell requests via http.
我参加了编码竞赛,任务是创建一个 RESTful 在线市场,用户可以在其中通过 http 发布买卖请求。
I need to build a front end web service that accepts and stores these requests.
我需要构建一个接受和存储这些请求的前端 Web 服务。
The tech requirements include both Spring boot and CXF. As far as I am aware, both CXF and Spring boot are capable of accepting http requests.
技术要求包括 Spring boot 和 CXF。据我所知,CXF 和 Spring boot 都能够接受 http 请求。
In spring boot, you use a controller like:
在 Spring Boot 中,您使用如下控制器:
@Controller
@EnableAutoConfiguration
public class controller {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello, World!";
}
}
Whereas with CXF (using javax.ws.rs), the code might look like this:
而使用 CXF(使用 javax.ws.rs),代码可能如下所示:
@WebService(serviceName = "MarketService", targetNamespace = "http://localhost:9005")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public interface MarketService {
@GET
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces({ MediaType.APPLICATION_JSON })
@Path("/sells/{id}")
public prod getProduct(@PathParam("id") int id);
Can someone help me understand the fundamental difference between these two approaches to handling http requests? Is there a way I can use both Spring Boot and CXF in the same application?
有人可以帮助我理解这两种处理 http 请求的方法之间的根本区别吗?有没有办法可以在同一个应用程序中同时使用 Spring Boot 和 CXF?
回答by Christophe L
Spring MVC and Apache CXF are 2 separate frameworks to handle HTTP requests and that can be used to build REST web services.
Spring MVC 和 Apache CXF 是两个独立的框架,用于处理 HTTP 请求,可用于构建 REST Web 服务。
- Spring MVCis a project under the Spring "umbrella" (and therefore strongly tied to the Spring framework on top of which it's built),
- Apache CXFis a open-source implementation of JAX-RS (REST) and JAX-WS (SOAP). Apache CXF can be run standalone or be included in a Spring application.
- Spring MVC是 Spring“保护伞”下的一个项目(因此与构建它的 Spring 框架紧密相关),
- Apache CXF是 JAX-RS (REST) 和 JAX-WS (SOAP) 的开源实现。Apache CXF 可以独立运行或包含在 Spring 应用程序中。
If you are looking to build a REST web service, they are pretty much mutually exclusive (you have to pick one). If all you're going to do is build REST web services, then they're pretty much equivalent. If you also need to have an MVC framework to serve HTML pages, then Spring MVC has that capability (CXF does not).
如果您正在寻找构建 REST Web 服务,它们几乎是相互排斥的(您必须选择一个)。如果您要做的只是构建 REST Web 服务,那么它们几乎是等效的。如果您还需要一个 MVC 框架来为 HTML 页面提供服务,那么 Spring MVC 具有该功能(CXF 没有)。
Personal opinion: Spring MVC is easier to get started with (thanks to Spring Boot which handles most of the configuration for you) than CXF (which requires more XML configuration).
个人意见:Spring MVC 比 CXF(需要更多 XML 配置)更容易上手(感谢 Spring Boot 为您处理大部分配置)。
PS: in your CXF example, you have a @WebServiceannotation. This annotation is part of JAX-WS (SOAP), not JAX-RS (REST). You probably don't need it.
PS:在您的 CXF 示例中,您有一个@WebService注释。此注释是 JAX-WS (SOAP) 的一部分,而不是 JAX-RS (REST)。你可能不需要它。
回答by David Blevins
Check this project out for nice starter for JAX-RS (REST) that leverages CXF on Tomcat via TomEE.
查看此项目,了解 JAX-RS (REST) 的良好启动器,它通过 TomEE 在 Tomcat 上利用 CXF。
Everything is all setup and ready to go.
一切都已准备就绪,可以开始了。
Long description here:
详细说明在这里:
Note, running CXF "Standalone" still requires a Servlet container (Tomcat or Jetty), so the above is several steps completed, simplified and finished in a small starter project. Designed for impatient people (like myself) that don't like to read directions and just like to start hacking. Always easier for me to start with something that works and then tweak it.
注意,运行CXF“Standalone”仍然需要一个Servlet容器(Tomcat或Jetty),所以以上几个步骤完成,简化,在一个小的starter工程中完成。专为不喜欢阅读说明而只想开始黑客攻击的不耐烦的人(如我自己)设计。对我来说总是更容易从有效的东西开始,然后再调整它。
回答by Dennis Kieselhorst
Use the Spring Boot CXF JAX-RS starter by adding:
通过添加以下内容来使用 Spring Boot CXF JAX-RS 启动器:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.1.7</version>
</dependency>

