JAX-RS 和 Spring Rest 的区别 [英] Difference between JAX-RS and Spring Rest

查看:36
本文介绍了JAX-RS 和 Spring Rest 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 JAX-RS(好吧,也许应该使用 Jersey 进行比较,因为 JAX-RS 只是规范)和 Spring for Restful 服务 之间的区别感到困惑.我试图在网上搜索更多信息,但它变得更加混乱.我公司正在使用Spring MVC开发Restful APIs

I confused with the difference between JAX-RS (well, maybe should use Jersey to do comparison since JAX-RS is just spec) and Spring for Restful services. I tried to search for more information online and it become more confusing. My company is using Spring MVC to develop Restful APIs

令人困惑的部分是,JAX-RS 代表 Java API for RESTful Web Services,在 Spring 中我也使用 java 来开发 RESTful Web 服务,所以我实际上没有得到差异.Spring 是否遵循 JAX-RS 规范?

The confusing part is, JAX-RS stands for Java API for RESTful Web Services, in Spring i am also using java to develop RESTful Web Services, so i don't actually get the differences. Does Spring follow the JAX-RS specifications?

据我所知:

  1. JAX-RS 是一个蓝图/规范,它有 Jersey、RESTeasy 等作为实现.

推荐答案

JAX-RS

JAX-RS 是一个 规范 用于在 Java 中实现 REST Web 服务,目前由 JSR-370.它是 Java EE 技术 的一部分,目前由 JSR 366.

JAX-RS

JAX-RS is a specification for implementing REST web services in Java, currently defined by the JSR-370. It is part of the Java EE technologies, currently defined by the JSR 366.

Jersey(随 GlassFish 和 Payara 一起提供)是 JAX-RS 参考实现,但还有其他实现例如 RESTEasy(与 JBoss EAP 和 WildFly 一起提供)和 Apache CXF(随 TomEE 和 WebSphere 一起提供).

Jersey (shipped with GlassFish and Payara) is the JAX-RS reference implementation, however there are other implementations such as RESTEasy (shipped with JBoss EAP and WildFly) and Apache CXF (shipped with TomEE and WebSphere).

Spring 框架是一个 完整框架,允许您创建 Java 企业应用程序.REST 功能由 Spring MVC 模块(提供 model-view-controller 功能的相同模块).它不是 JAX-RS 实现,可以看作是 JAX-RS 标准的 Spring 替代方案.

The Spring Framework is a full framework that allows you to create Java enterprise applications. The REST capabilities are provided by the Spring MVC module (same module that provides model-view-controller capabilities). It is not a JAX-RS implementation and can be seen as a Spring alternative to the JAX-RS standard.

Spring 生态系统还提供了广泛的项目用于创建企业应用程序,涵盖持久性、安全性、与社交网络、批处理等

The Spring ecosystem also provides a wide range of projects for creating enterprise applications, covering persistence, security, integration with social networks, batch processing, etc.

考虑以下使用 JAX-RS API 的资源控制器:

Consider the following resource controller using the JAX-RS API:

@Path("/greetings")
public class JaxRsController {

    @GET
    @Path("/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response greeting(@PathParam("name") String name) {

        String greeting = "Hello " + name;
        return Response.ok(greeting).build();
    }
}

使用 Spring MVC API 的等效实现是:

The equivalent implementation using the Spring MVC API would be:

@RestController
@RequestMapping("/greetings")
public class SpringRestController {

    @RequestMapping(method = RequestMethod.GET,
                    value = "/{name}", 
                    produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<?> greeting(@PathVariable String name) {

        String greeting = "Hello " + name;
        return new ResponseEntity<>(greeting, HttpStatus.OK);
    }
}

使用 Spring Boot 和 Jersey

Spring Boot 提供了 spring-boot-starter-jersey 模块,允许您将 JAX-RS 编程模型用于 REST 端点,而不是 Spring MVC.它适用于 Jersey 2.x.

Using Spring Boot and Jersey

Spring Boot provides the spring-boot-starter-jersey module that allows you to use the JAX-RS programming model for the REST endpoints instead of Spring MVC. It works quite well with Jersey 2.x.

有关使用 Jersey 2.x 和 Spring Boot 1.4.x 创建 Web 应用程序的完整示例,请参阅此答案.

For a complete example of creating a web application with Jersey 2.x and Spring Boot 1.4.x, refer to this answer.

这篇关于JAX-RS 和 Spring Rest 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆