Spring Boot + Jersey
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的更多相关文章
- Spring Boot + Jersey发生FileNotFoundException (No such file or directory)
我在使用Spring Boot + Jersey 项目,解决了上一篇随笔中的FileNotFoundException,然后又报了一个FileNotFoundException,不过报错信息不一样了 ...
- spring boot + jersey工程由jar包转为war包在tomcat中启动报错问题
第一步: 在maven下,将Spring Boot工程由jar转换为war包启动,很简单,将pom.xml文件中的packaging改为war <packaging>war</pac ...
- Spring Boot Jersey使用示例
前言 本文将学习如何使用Spring Boot和Jersey框架,去配置和创建JAX-RS 2.0 REST API接口: 这个示例应用使用的是Jersey的Servlet容器去部署REST API接 ...
- Spring Boot通过命令行启动发生FileNotFoundException
Spring Boot + Jersey 通过命令行启动会发生错误FileNotFoundException异常 异常信息如下: ERROR o.a.c.c.C.[Tomcat].[localhost ...
- jersey在 spring boot 添加 packages 扫描路径支持
最近公司内部系统要做数据对接,故使用 jersey 来做 restful webservice 接口设计.由于 spring boot 已经集成 jersey,估计直接导入 spring-boot-s ...
- 【spring boot】SpringBoot初学(5)– WebService之Jersey
前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置: 一.准备 spring boot对jersey1.x与jersey2.x的注入方 ...
- 玩转spring boot——properties配置
前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...
- spring boot实战(第十三篇)自动配置原理分析
前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...
- Spring Boot的启动器Starter详解
Spring Boot的启动器Starter详解 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs Spring Boot ...
随机推荐
- JavaScript版MD5应用
假设使用Java为一个字符串进行MD5操作,我想轻而易举:但是假设要在client为网页中字符串进行MD5操作又该怎么做呢? 代码1--为字符串等进行MD5操作的js文件(md5.js): /* * ...
- nagios插件之登陆防火墙实现session监控
ssh_firewall_session.sh -- 登陆防火墙并运行dis session statistics firewall_check_sessions.c -- 调用上面脚本.过滤出ses ...
- Boolean operations between triangle meshes
Boolean operations between triangle meshes eryar@163.com Abstract. Boolean operations is one of basi ...
- js中常用的对象—Array的属性和方法
今天说一下,js中常用的内置对象——Array对象 Array常用属性: length prototype :给系统对象添加属性和方法 Array常用方法: Array.prototype.sum = ...
- Kinect 开发 —— 全息图
Kinect的另一个有趣的应用是伪全息图(pseudo-hologram).3D图像可以根据人物在Kinect前面的各种位置进行倾斜和移动.如果方法够好,可以营造出3D控件中3D图像的效果,这样可以用 ...
- web知识—协议
web使用超文本传输协议(HTTP,HyperText Transfer Protocol)进行通信.http在1990年左右出现,现在有0.9/1.0/1.1三个版本.在早期的互联网中的一些协议只能 ...
- C# 读取指定文件夹中的全部文件,并按规则生成SQL语句!
本实例的目的在于: 1 了解怎样遍历指定文件夹中的全部文件 2 控制台怎样输入和输出数据 代码: using System; using System.IO; namespace ToSql{ cla ...
- Android app : use html or native?
Android app可分为两种:网络(html)应用程序和原生(native)应用程序 首先,我们先来讨论下如何判断一个app是html实现还是native实现. 设置-->>开发者选项 ...
- 使用Notepad++的XML Tools插件格式化XML文件
转自“”:https://blog.csdn.net/qq_36279445/article/details/79803310 1. 安装XML Tools插件 (1) 通过网址http://sour ...
- 1.java soap api操作和发送soap消息
转自:https://blog.csdn.net/lbinzhang/article/details/84721359 1. /** * soap请求 * * @return * @throws Ex ...