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

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

问题描述

我对 JAX-RS (嗯,由于JAX-RS只是规格,也许应该使用Jersey进行比较)和 Spring for Restful services 之间的区别感到困惑.我试图在线搜索更多信息,但变得更加混乱.我公司正在使用Spring MVC开发Restful API

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代表 RESTful Web Services的Java API ,在春季,我也使用Java开发RESTful Web Services,所以我实际上并没有区别. 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技术的一部分. org/en/jsr/detail?id = 366"rel =" noreferrer> 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附带)和

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框架 Spring MVC 模块(提供 model-view-controller 功能的同一模块).它不是JAX-RS的实现,可以看作是JAX-RS stardard的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 stardard.

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天全站免登陆