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);
        }
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`spring-boot-starter-jersey` 是 Spring Boot 官方提供的一个集成 JAX-RS (Jersey) 的 Starter,通过引入该 Starter 可以很方便的集成 JAX-RS 技术栈。 Jersey 是 JAX-RS 的一个参考实现,它提供了一个基于 Servlet 的 Web 容器,使得开发人员可以很容易地创建 RESTful Web 服务。在使用 Spring Boot 集成 Jersey 时,我们只需要引入 `spring-boot-starter-jersey` 依赖就可以了。 下面是一个示例,演示了如何在 Spring Boot 中使用 Jersey: 1. 添加依赖 在 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> ``` 2. 创建 REST 资源类 创建一个 REST 资源类,并使用 `@Path` 和 `@Produces` 注解定义资源路径和返回数据的类型。 ```java import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("hello") public class HelloResource { @GET @Produces(MediaType.TEXT_PLAIN) public String sayHello() { return "Hello, World!"; } } ``` 3. 创建 Application 类 创建一个 `Application` 类,并使用 `@ApplicationPath` 注解定义应用程序的根路径。 ```java import org.glassfish.jersey.server.ResourceConfig; import org.springframework.stereotype.Component; import javax.ws.rs.ApplicationPath; @Component @ApplicationPath("/api") public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(HelloResource.class); } } ``` 4. 启动应用程序并测试 启动应用程序,访问 URL http://localhost:8080/api/hello,您应该可以看到 "Hello, World!" 这个字符串。 这就是在 Spring Boot 中集成 Jersey 的基本步骤。您可以根据您的需求,使用更多的 JAX-RS 注解来定义 RESTful 资源类的行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值