JAVA基础 - JERSEY简介

JERSEY是一个基于JAVA的轻量级RESTful服务框架,它是JAX-RS(JSR311)的开源参考实现。除了提供JAX-RS标准接口外,JERSEY还有自己的API,简化了REST服务和客户端的开发。JETTY是一个与TOMCAT类似的SERVLET容器,常与JERSEY结合用于构建Web服务。文章中给出了使用JERSEY和JETTY构建Web服务的依赖引入及服务端代码示例。
摘要由CSDN通过智能技术生成

是什么

JERSEY是基于JAVA的、轻量级的、RESTful风格的WEB SERVICES框架,是JAX-RS(JSR311)开源参考实现。 JERSEY框架不只是JAX-RS参考实现,还提供了自己的API,进一步简化了RESTful服务和客户端开发。
JERSEY官网:https://eclipse-ee4j.github.io/jersey
用户手册:https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest31x/index.html

术语

(1)什么是SOAP?SOAP是简单对象访问协议的缩写。它是一种基于XML的消息传递协议,用于在计算机之间交换信息。
(2)什么是REST?REST是表述性状态传递的缩写,也就是资源在网络中以某种表现形式进行状态转移。它是一种软件架构风格,描述的是在网络中C和S的一种交互形式。
(3)什么是RESTful?REST 是一组架构约束条件和原则,而满足这些约束条件和原则的应用程序或设计就是 RESTful。
(4)什么是WEB SERVICES?WEB SERVICES是一种标准,他可以通过SOAP或REST的方式来实现。

JETTY是什么?是SERVLET容器(和TOMCAT类似)。

如何使用

JETTY + JERSEY 可以构建WEB服务。

① 依赖引入
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-core</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.19</version>
</dependency>

<!-- Jersey + Spring -->
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-spring</artifactId>
    <version>1.19.4</version>
    <exclusions>
       <exclusion>
           <groupId>org.springframework</groupId>
           <artifactId>spring</artifactId>
       </exclusion>
       <exclusion>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
       </exclusion>
       <exclusion>
           <groupId>org.springframework</groupId>
           <artifactId>spring-web</artifactId>
       </exclusion>
       <exclusion>
           <groupId>org.springframework</groupId>
           <artifactId>spring-beans</artifactId>
       </exclusion>
       <exclusion>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
       </exclusion>
   </exclusions>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.19.4</version>
</dependency>
② 服务端代码
@Path("v2/relationship")
@Singleton
@Service
@Consumes({Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON})
@Produces({Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON})
public class RelationshipREST {
    private static final Logger PERF_LOG = AtlasPerfTracer.getPerfLogger("rest.RelationshipREST");
    private final AtlasRelationshipStore relationshipStore;
    @Inject
    public RelationshipREST(AtlasRelationshipStore relationshipStore) {
        this.relationshipStore = relationshipStore;
    }

    @POST
    public AtlasRelationship create(AtlasRelationship relationship) throws AtlasBaseException {
        AtlasPerfTracer perf = null;
        try {
            if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
                perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "RelationshipREST.create(" + relationship + ")");
            }
            return relationshipStore.create(relationship);
        } finally {
            AtlasPerfTracer.log(perf);
        }
    }

    @PUT
    public AtlasRelationship update(AtlasRelationship relationship) throws AtlasBaseException {
        AtlasPerfTracer perf = null;
        try {
            if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
                perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "RelationshipREST.update(" + relationship + ")");
            }
            return relationshipStore.update(relationship);
        } finally {
            AtlasPerfTracer.log(perf);
        }
    }
    
    @GET
    @Path("/guid/{guid}")
    public AtlasRelationshipWithExtInfo getById(@PathParam("guid") String guid,
                                                @QueryParam("extendedInfo") @DefaultValue("false") boolean extendedInfo)
                                                throws AtlasBaseException {
        Servlets.validateQueryParamLength("guid", guid);
        AtlasPerfTracer perf = null;
        AtlasRelationshipWithExtInfo ret;
        try {
            if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
                perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "RelationshipREST.getById(" + guid + ")");
            }
            if (extendedInfo) {
                ret = relationshipStore.getExtInfoById(guid);
            } else {
                ret = new AtlasRelationshipWithExtInfo(relationshipStore.getById(guid));
            }
            return ret;
        } finally {
            AtlasPerfTracer.log(perf);
        }
    }
    @DELETE
    @Path("/guid/{guid}")
    public void deleteById(@PathParam("guid") String guid) throws AtlasBaseException {
        Servlets.validateQueryParamLength("guid", guid);
        AtlasPerfTracer perf = null;
        try {
            if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
                perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "RelationshipREST.deleteById(" + guid + ")");
            }
            relationshipStore.deleteById(guid);
        } finally {
            AtlasPerfTracer.log(perf);
        }
    }
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cloneme01

谢谢您的支持与鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值