简体   繁体   English

Spring Boot与Apache CXF的RESTful Web服务?

[英]Spring Boot vs. Apache CXF for RESTful Web Services?

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. 技术要求包括Spring boot和CXF。 As far as I am aware, both CXF and Spring boot are capable of accepting http requests. 据我所知,CXF和Spring启动都能够接受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? 有人可以帮我理解这两种处理http请求的方法之间的根本区别吗? Is there a way I can use both Spring Boot and CXF in the same application? 有没有办法在同一个应用程序中同时使用Spring Boot和CXF?

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 MVC is a project under the Spring "umbrella" (and therefore strongly tied to the Spring framework on top of which it's built), Spring MVCSpring下的一个项目“伞”(因此与它构建的Spring框架紧密相关),
  • Apache CXF is a open-source implementation of JAX-RS (REST) and JAX-WS (SOAP). Apache CXF是JAX-RS(REST)和JAX-WS(SOAP)的开源实现。 Apache CXF can be run standalone or be included in a Spring application. Apache CXF可以独立运行,也可以包含在Spring应用程序中。

If you are looking to build a REST web service, they are pretty much mutually exclusive (you have to pick one). 如果您正在寻求构建REST Web服务,它们几乎是互斥的(您必须选择一个)。 If all you're going to do is build REST web services, then they're pretty much equivalent. 如果您要做的就是构建REST Web服务,那么它们就完全相同了。 If you also need to have an MVC framework to serve HTML pages, then Spring MVC has that capability (CXF does not). 如果你还需要一个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). 个人观点 :与CXF(需要更多XML配置)相比,Spring MVC更易于入门(感谢Spring Boot处理大部分配置)。

PS: in your CXF example, you have a @WebService annotation. PS:在您的CXF示例中,您有一个@WebService注释。 This annotation is part of JAX-WS (SOAP), not JAX-RS (REST). 此批注是JAX-WS(SOAP)的一部分,而不是JAX-RS(REST)。 You probably don't need it. 你可能不需要它。

Check this project out for nice starter for JAX-RS (REST) that leverages CXF on Tomcat via TomEE. 检查这个项目是否适合通过TomEE在Tomcat上利用CXF的JAX-RS(REST)。

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. 注意,运行CXF“Standalone”仍然需要一个Servlet容器(Tomcat或Jetty),因此上面几个步骤已完成,简化并在一个小型启动项目中完成。 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. 我总是更容易从有用的东西开始,然后调整它。

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>

See also: http://cxf.apache.org/docs/springboot.html 另见: http//cxf.apache.org/docs/springboot.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM