1. Overview
1.概述
In this tutorial, we’ll see the difference between JAX-RS and Spring MVC for REST API development.
在本教程中,我们将看到用于REST API开发的JAX-RS和Spring MVC之间的区别。
2. Jakarta RESTful Web Services
2.雅加达RESTful网络服务
To become part of the JAVA EE world, a feature must have a specification, a compatible implementation, and a TCK. Accordingly, JAX-RS is a set of specifications for building REST services. Its best-known reference implementations are RESTEasy and Jersey.
要成为JAVA EE世界的一部分,一个功能必须有一个规范、一个兼容的实现和TCK。因此,JAX-RS是一套用于构建 REST 服务的规范。其最知名的参考实现是RESTEasy和Jersey。
Now, let’s get a little familiar with Jersey by implementing a simple controller:
现在,让我们通过实现一个简单的控制器来熟悉一下Jersey的情况。
@Path("/hello")
public class HelloController {
@GET
@Path("/{name}")
@Produces(MediaType.TEXT_PLAIN)
public Response hello(@PathParam("name") String name) {
return Response.ok("Hello, " + name).build();
}
}
Above, the endpoint returns a simple “text/plain” response as the annotation @Produces states. Particularly, we are exposing a hello HTTP resource that accepts a parameter called name with two @Path annotations. We also need to specify that it is a GET request, using the annotation @GET.
上面,端点返回一个简单的 “text/plain “响应,正如注解@Produces所说。特别是,我们正在公开一个hello HTTP资源,它接受一个名为name的参数,并有两个@Path注解。我们还需要使用注解@GET来指定它是一个GET请求。
3. REST With Spring MVC
3.使用Spring MVC的REST
Spring MVC is a module of Spring Framework for creating web applications. It adds REST capability to Spring Framework.
Spring MVC是Spring框架的一个模块,用于创建Web应用程序。它为Spring框架增加了REST功能。。
Let’s make the equivalent GET request example as above, using Spring MVC:
让我们用Spring MVC做一个与上面相同的GET请求例子。
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping(value = "/{name}", produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<?> hello(@PathVariable String name) {
return new ResponseEntity<>("Hello, " + name, HttpStatus.OK);
}
}
Looking at the code, @RequestMapping states that we’re dealing with a hello HTTP resource. In particular, through the @GetMapping annotation, we’re specifying that it is a GET request. It accepts a parameter called name and returns a “text/plain” response.
看一下代码,@RequestMapping指出我们正在处理一个hello HTTP资源。特别是,通过@GetMapping注解,我们指定它是一个GET请求。它接受一个名为name的参数,并返回一个 “text/plain “响应。
4. Differences
4.差异
JAX-RS hinges on providing a set of Java Annotations and applying them to plain Java objects. Indeed, those annotations help us to abstract the low-level details of the client-server communication. To simplify their implementations, it offers annotations to handle HTTP requests and responses and bind them in the code. JAX-RS is only a specification and it needs a compatible implementation to be used.
JAX-RS的关键在于提供一组Java注解,并将其应用于普通的Java对象。事实上,这些注解帮助我们抽象出客户端-服务器通信的低级细节。为了简化它们的实现,它提供了处理HTTP请求和响应的注解,并在代码中绑定它们。JAX-RS只是一个规范,它需要一个兼容的实现才能使用。
On the other hand, Spring MVC is a complete framework with REST capabilities. Like JAX-RS, it also provides us with useful annotations to abstract from low-level details. Its main advantage is being a part of the Spring Framework ecosystem. Thus, it allows us to use dependency injection like any other Spring module. Furthermore, it integrates easily with other components like Spring AOP, Spring Data REST, and Spring Security.
另一方面,Spring MVC是一个具有REST功能的完整框架。与JAX-RS一样,它也为我们提供了有用的注释,以抽象出低级别的细节。它的主要优势在于它是Spring Framework生态系统的一部分。因此,它允许我们像其他Spring模块一样使用依赖注入。此外,它可以轻松地与其他组件集成,如Spring AOP、Spring Data REST和Spring Security。
5. Conclusion
5.总结
In this quick article, we looked at the main differences between JAX-RS and Spring MVC.
在这篇快速文章中,我们看了JAX-RS和Spring MVC的主要区别。
As usual, the source code for this article is available over on GitHub.
像往常一样,本文的源代码可以在GitHub上找到。