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

查看:265
本文介绍了为什么要使用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?

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

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应用程序服务器(包括GlassFish,WebLogic,WebSphere,JBoss等)的RESTful服务.

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