Jersey是一个很好的Java REST API库。当你用Jersey实现REST的时候。是很自然的。同一时候Spring Boot是Java世界中还有一个很好的工具。它降低了程序的应用配置(《初识Spring Boot》)。这篇博客就介绍下怎样将Jersey和Spring
Boot结合起来使用。

须要注意的是Jersey本身自带了hk2这样一个DI库,所以,在结合Spring Boot使用的时候,是比較easy搞混淆的。简单的讲,你应该分清楚,哪一部分是由Spring来管理,哪一部分是由Jersey的hk2来管理。

创建一个JerseyConfig类,用来配置Jersey:

public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {

        register(JacksonFeature.class);

        

        register(ProductsResource.class);

        register(ProductRepository.class);

    }

}

当中,ProductsResource是我们的REST API:

@Path("/products")

public class ProductsResource {

    @Inject

    private ProductRepository productRepository;



    //... ...



    @GET

    @Path("{id}")

    @Produces(MediaType.APPLICATION_JSON)

    public ProductRefJson getProductById(@PathParam("id") int id) {

        final Product product = productRepository.findByProductId(id);

        if (product == null) {

            throw new ResourceNotFoundException();

        }

        return new ProductRefJson(product);

    }

}

ProductRespository就是我们查找Product的类,这里须要注意的一点是我们看到ProductsResource并不含有类似@Component这种annotation,这是由于这个类是REST API,它应该由Jersey中的hk2来管理,所以,不要加@Component。

这样,我们已经完毕了Jersey REST API所须要的最简配置。以下就是让Spring Boot怎么知道Jersey的存在:

@EnableAutoConfiguration

public class Application{

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class)

                .showBanner(false)

                .run(args);

    }



    @Bean

    public ServletRegistrationBean jerseyServlet() {

        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*");

        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());

        return registration;

    }

}

启动Application就能执行这个REST Webservie了:

curl -X GET http://localhost:8080/products/1

返回结果:

{"id":1,"name":"apple juice","uri":"/products/1","pricings_uri":"/products/1/pricings","current_price":{"uri":"/products/1/current","price":0},"description":"good"}

博客完毕的比較仓促,假设有描写叙述错误或者不准确的地方。欢迎指出来。

Spring Boot + Jersey的更多相关文章

  1. Spring Boot + Jersey发生FileNotFoundException (No such file or directory)

    我在使用Spring Boot + Jersey 项目,解决了上一篇随笔中的FileNotFoundException,然后又报了一个FileNotFoundException,不过报错信息不一样了 ...

  2. spring boot + jersey工程由jar包转为war包在tomcat中启动报错问题

    第一步: 在maven下,将Spring Boot工程由jar转换为war包启动,很简单,将pom.xml文件中的packaging改为war <packaging>war</pac ...

  3. Spring Boot Jersey使用示例

    前言 本文将学习如何使用Spring Boot和Jersey框架,去配置和创建JAX-RS 2.0 REST API接口: 这个示例应用使用的是Jersey的Servlet容器去部署REST API接 ...

  4. Spring Boot通过命令行启动发生FileNotFoundException

    Spring Boot + Jersey 通过命令行启动会发生错误FileNotFoundException异常 异常信息如下: ERROR o.a.c.c.C.[Tomcat].[localhost ...

  5. jersey在 spring boot 添加 packages 扫描路径支持

    最近公司内部系统要做数据对接,故使用 jersey 来做 restful webservice 接口设计.由于 spring boot 已经集成 jersey,估计直接导入 spring-boot-s ...

  6. 【spring boot】SpringBoot初学(5)– WebService之Jersey

    前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置: 一.准备 spring boot对jersey1.x与jersey2.x的注入方 ...

  7. 玩转spring boot——properties配置

    前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...

  8. spring boot实战(第十三篇)自动配置原理分析

    前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...

  9. Spring Boot的启动器Starter详解

    Spring Boot的启动器Starter详解 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs Spring Boot ...

随机推荐

  1. c/c++中static与extern关键字介绍

    一.C语言中的static关键字 在C语言中,static可以用来修饰局部变量,全局变量以及函数.在不同的情况下static的作用不尽相同. (1)修饰局部变量 一般情况下,对于局部变量是存放在栈区的 ...

  2. ElasticSearch 5.2.2 安装及 head 插件的安装

    ElasticSearch 是一个基于 Lucene 的高度可扩展的开源全文搜索和分析引擎.它能够做到可以快速.实时地存储.搜索和分析大量数据.它通常作为底层引擎/技术,为具有复杂搜索功能和要求的应用 ...

  3. PHP 7.1安装xhprof进行性能分析

    安装扩展该 xhprof扩展版本是从 https://github.com/longxinH/xhprof 获取的(第三方的一个库,官方版本不支持php7) 下载并编译xhprof扩展在web的htm ...

  4. csdn课堂学习

    http://edu.csdn.net/course/detail/2495?ref=blog&loc=0 http://edu.csdn.net/course/detail/2140/336 ...

  5. [PWA] Deal with caches in PWA

    The takeway is to know when we should cache the content? When we should clean the caches? 1. When sh ...

  6. Python和C|C++的混编(二):利用Cython进行混编

    还能够使用Cython来实现混编 1 下载Cython.用python setup.py install进行安装 2 一个实例 ① 创建helloworld文件夹 创建helloworld.pyx,内 ...

  7. google 分屏 横屏模式 按home键界面错乱故障分析(二) 分屏的启动过程

    google 进入分屏后在横屏模式按home键界面错乱(二) 你确定你了解分屏的整个流程? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240&quo ...

  8. java / C++ B+树代码

    C++ 代码 #include <> JAVA 代码 package org.test.me.struct; /** * author: shuly * create: 2018-09-1 ...

  9. php全局变量的使用

    php全局变量的使用 一.总结 1.php的全局变量:php的全局变量和C++,Java的全局变量不一样 2.页面嵌套php:我在html页面中嵌套php代码的时候,php的全局变量好像和C++,Ja ...

  10. Flume的核心概念

    Event:一条数据  Client:生产数据,运行在一个独立的线程. Agent  (1)Sources.Channels.Sinks  (2)其他组件:Interceptors.Channel S ...