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. java.util.ConcurrentModificationException 异常解决的方法及原理

    近期在修程序的bug,发现后台抛出下面异常: Exception in thread "main" java.util.ConcurrentModificationExceptio ...

  2. 自编Photoshop简单教程

    由于本科时对图形图像比較感兴趣所以Ps和Ai玩的还算能够.所以无论本科时候还是研究生阶段总是有非常多人让我帮忙处理一些图片.记得工作那一年參与一个大项目时还帮了CRI里员工处理了一些图片项目中也处理了 ...

  3. smarty课程---最最最简单的smarty例子

    smarty课程---最最最简单的smarty例子 一.总结 一句话总结:其实所有的模板引擎的工作原理是差不多的,无非就是在php程序里面用正则匹配将模板里面的标签替换为php代码从而将两者混合为一个 ...

  4. css3--简单的加载动画

    .load-container { width: 30%; height: auto; position: relative; margin: 1rem auto; } .load { width: ...

  5. 关于vue.js中v-model与表单控件的双向绑定。

    单选框:<input type="checkbox" id="checkbox" v-model="checked"><l ...

  6. Spring boot 解析jsp支持jsp热部署

    解析jsp并且支持jsp热部署 参考地址:https://www.wanpishe.top/detail?blogId=39875536-7d45-48ec-a7b5-f36b85c3a235

  7. Ansible学习记录五:PlayBook学习

    0.介绍 Playbooks 是 Ansible 管理配置.部署应用和编排的语言,可以使用 Playbooks 来描述你想在远程主机执行的策略或者执行的一组步骤过程等 类似于一组任务集,定义好像项目, ...

  8. logback 生成日志

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender ...

  9. 47.Android 自己定义PopupWindow技巧

    47.Android 自己定义PopupWindow技巧 Android 自己定义PopupWindow技巧 前言 PopupWindow的宽高 PopupWindow定位在下左位置 PopupWin ...

  10. php课程 12-38 php的类的构造方法和析构方法怎么写

    php课程 12-38 php的类的构造方法和析构方法怎么写 一.总结 一句话总结:a.__construct(参数){},__destruct(){},b.如果类中的一个方法和类名相同,则该方法为构 ...