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 ...
随机推荐
- AVEVA PDMS 三维文字工具
AVEVA PDMS 三维文字工具 eryar@163.com 网上有个文字工具插件,可以在PDMS中创建三维的字母和数字,且字体样式只有一种,其下载地址为:http://www.plantcon.d ...
- Linux智能手机安全策略研究
Linux智能手机安全策略研究 http://www.zdnet.com.cn 本文是继从“窃听门”事件解读手机Rootkit攻击(http://chenguang.blog.51cto.com ...
- 【基础篇】EditText的一些属性设置
设置EditText的背景颜色 private test_editText=null; test_editText= (EditText) findViewById(R.id.EditTextInp ...
- BZOJ1814: Ural 1519 Formula 1(插头Dp)
Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...
- iptables-save && iptables-restore iptables规则保存于还原
iptables-save命令用于将linux内核中的iptables表导出到标准输出设备商,通常,使用shell中I/O重定向功能将其输出保存到指定文件中. 语法 -t:指定要保存的表的名称. 实例 ...
- web知识—协议
web使用超文本传输协议(HTTP,HyperText Transfer Protocol)进行通信.http在1990年左右出现,现在有0.9/1.0/1.1三个版本.在早期的互联网中的一些协议只能 ...
- 从硬件到语言,详解C++的内存对齐(memory alignment)(一)
作者:赵宗晟 出处:https://www.cnblogs.com/zhao-zongsheng/p/9099603.html 很多写C/C++的人都知道“内存对齐”的概念以及规则,但不一定对他有很深 ...
- HTTP (httpwebrequest)
1.GET请求: public static string Get(string url) { string buffer = ""; try { HttpWebRequest r ...
- Chrome不能在网易网盘中上传文件的解决办法
Chrome不能在网易网盘中上传文件的解决办法1. 安装 Adobe Flash Player PPAPI,设置flash插件 chrome://settings/content/flash,许可[* ...
- js配合My97datepicker给日期添加天数
<input name="ctl00$ContentPlaceHolder1$txtTimeStart" type="text" value=" ...