为什么使用 JAX-RS/Jersey? [英] Why use JAX-RS / Jersey?

查看:24
本文介绍了为什么使用 JAX-RS/Jersey?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,这个问题听起来很傻,但是在使用 Jersey 开发了一些 RESTful 服务之后,我问自己一个问题——如果 REST 只是一种架构,而不是像 SOAP 这样的协议,为什么我们需要像 JAX 这样的规范-RS?

Sorry, this questions sounds silly, but after developing some of my RESTful services using Jersey, I asked myself the question -- If REST is just an architecture, and not a protocol like SOAP, why do we need a specification like JAX-RS?

我实际上搜索了诸如servlet 和 RESTful 服务通过 HTTP 有什么区别"之类的问题,并总结了社区的答案,我得到了:

I actually googled for questions like "What is the difference between servlets and RESTful services over HTTP" and to sum up the community answers, I got:

  1. RESTful 服务开发(在 Jersey 上)是一种架构,它本质上使用 servlet.
  2. Jersey 等符合 JAX-RS 的工具可轻松编组和解组 XML/JSON 数据,从而帮助开发人员.
  3. REST 帮助我们以一种比普通 servlet 高效得多的方式使用 GET/POST/PUT/DELETE.

根据这些答案,我想如果我编写一个使用 JAXB(用于处理自动序列化)的 servlet,并且我在我的 servlet 代码中有效地使用 GET/POST/PUT/DELETE,我不会使用像泽西岛,因此是 JAX-RS.

According to these answers, I guess if I write a servlet which uses JAXB (for dealing with automatic serialization), and I efficiently use GET/POST/PUT/DELETE in my servlet code, I don't use a tool like Jersey, and hence JAX-RS.

我知道我通过这个声明是非常错误的,请纠正我.

I know I am terribly wrong passing this statement, please correct me.

PS:当我不得不用 PHP 开发一些 RESTful 服务时,这个疑问实际上就出现了.在浏览了一些 RESTful PHP 代码之后,我意识到它们只是相同的旧 PHP 脚本,带有一些用于处理 XML/JSON 的辅助方法.

PS: This doubt actually came in when I had to develop some RESTful services in PHP. After going on through some of the RESTful PHP codes, I realized they are just the same old PHP scripts, with some helper methods for handling XML/JSON.

推荐答案

为什么要使用 JAX-RS/Jersey?

Why use JAX-RS / Jersey?

简答

因为它使 RESTful 服务的开发更容易.

Because it makes the development of RESTful services easier.

长答案

JAX-RS 是一种标准,可以轻松创建可部署到任何 Java 应用服务器的 RESTful 服务:GlassFish、WebLogic、WebSphere、JBoss 等.

JAX-RS is a standard that makes it easy to create a RESTful service that can be deployed to any Java application server: GlassFish, WebLogic, WebSphere, JBoss, etc.

JAX-RS 是 Java EE 的一部分,当 JAX-RS 与其他 Java EE 技术一起使用时,创建 RESTful 服务变得更加容易:

JAX-RS is part of Java EE, and when JAX-RS is used with other Java EE technologies it becomes even easier to create your RESTful service:

  • EJB - 会话 bean 用作服务实现并处理事务语义.
  • JAX-RS - 用于将会话 bean 公开为 RESTful 服务
  • JPA - 用于将 POJO 持久化到数据库中.请注意如何将 EntityManager 注入会话 bean.
  • JAXB - 用于将 POJO 转换为 XML(在 GlassFish 中它也可用于将 POJO 转换为 JSON).默认情况下,JAX-RS 处理与 JAXB 实现的交互.
  • EJB - A session bean is used as the service implementation and also handles the transaction semantics.
  • JAX-RS - Used to expose the session bean as a RESTful service
  • JPA - Used to persist the POJOs to the database. Note how the EntityManager is injected onto the session bean.
  • JAXB - Used to convert the POJO to/from XML (in GlassFish it can also be used to convert the POJO to/from JSON). JAX-RS by default handles the interaction with the JAXB implementation.

示例 JAX-RS 服务

package org.example;

import java.util.List;

import javax.ejb.*;
import javax.persistence.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Stateless
@LocalBean
@Path("/customers")
public class CustomerService {

    @PersistenceContext(unitName="CustomerService",
                        type=PersistenceContextType.TRANSACTION)
    EntityManager entityManager;

    @POST
    @Consumes(MediaType.APPLICATION_XML)
    public void create(Customer customer) {
        entityManager.persist(customer);
    }

    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("{id}")
    public Customer read(@PathParam("id") long id) {
        return entityManager.find(Customer.class, id);
    }

    @PUT
    @Consumes(MediaType.APPLICATION_XML)
    public void update(Customer customer) {
        entityManager.merge(customer);
    }

    @DELETE
    @Path("{id}")
    public void delete(@PathParam("id") long id) {
        Customer customer = read(id);
        if(null != customer) {
            entityManager.remove(customer);
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("findCustomersByCity/{city}")
    public List<Customer> findCustomersByCity(@PathParam("city") String city) {
        Query query = entityManager.createNamedQuery("findCustomersByCity");
        query.setParameter("city", city);
        return query.getResultList();
    }

}

更多信息:

这篇关于为什么使用 JAX-RS/Jersey?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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