JAVA基础 - JERSEY简介

是什么

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);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/goodjava2007/article/details/131068933