前言

github: https://github.com/vergilyn/SpringBootDemo

代码位置:


一、准备

spring boot对jersey1.x与jersey2.x的注入方式有区别。本文是针对2.x的配置(服务端,不包含客户端调用。)

需要依赖的POMs

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

jersey的配置与别的不同的是:需要一个ResourceConfig类型的@Bean,用于注册所有的端点(endpoints)。

二、demo

2.1 用于注册所有endpoints的Config
/* 想要开始使用Jersey 2.x只需要加入spring-boot-starter-jersey依赖,
* 然后你需要一个ResourceConfig类型的@Bean,用于注册所有的端点(endpoints,demo为JerseyController)。
*/
//@Component
@Configuration
//Jersey servlet将被注册,并默认映射到/*。可将@ApplicationPath添加到ResourceConfig来改变该映射。
@ApplicationPath("/rest")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(JerseyController.class);
// packages("com.vergilyn.demo.springboot.jersey"); // 通过packages注册。
}
}
2.2 endpoints
/*
* 所有注册的端点都应该被@Components和HTTP资源annotations(比如@GET)注解。
* 1、因为是@Component,所以其生命周期受Spring管理。
* 并且你可以使用@Autowired添加依赖及使用@Value注入外部配置。
*/
//@Component
@RestController
@Path("/jersey")
public class JerseyController {
@GET
@Path("/get")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> getMessage() {
return Constant.map;
} @POST //POST形式在浏览器地址栏输入请求路径不一定能访问到。推荐用fiddler工具或者firefox浏览器插件(poster或HttpRequester) @Path("/post")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> postMessage() {
return Constant.map;
}
}

到此需要注意的有:

1. 如果是post形式,浏览器不一定可直接访问得到json。最好用Fiddler工具或者FireFox浏览器插件(Poster或HttpRequester)测试接口。

2. 既然是RESTful,所以建议直接用@RestController,而并不建议使用@Controller。

3. 所有注册的端点都应该被@Components和HTTP资源annotations(比如@GET)注解。

2.3 SpringApplication
@SpringBootApplication
public class JerseyApplication {
/* 代码注入:
* 此种方式需注意:ServletRegistrationBean的配置,及最终的请求路径。
* 注解注入:
* JerseyConfig.java中用@Configuration
*/
// @Bean
public ServletRegistrationBean jerseyServlet() {
/* 特别注意此路径,与JerseyController中的@Path。可能让最终路径变成:localhost:8080/rest/jersey/get
* rest是此ServletRegistrationBean定义的(同ResourceConfig的类注解@ApplicationPath("/rest"))
* jersey是Controller中类注解@Path定义的
*/
ServletRegistrationBean registration = new ServletRegistrationBean(
new ServletContainer(), "/rest/*");
// our rest resources will be available in the path /rest/*
registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS,
JerseyConfig.class.getName());
return registration;
} public static void main(String[] args) {
SpringApplication.run(JerseyApplication.class, args);
}
}

到此,所有的jersey2.X服务端代码就算完成。

如果是get请求,那么浏览器直接请求:localhost:8080/rest/jersey/get 就可以得到返回的json结果。(虽然代码中get返回的是Map,但定义@Produces(MediaType.APPLICATION_JSON))

至于请求参数的接收、客户端的调用,和spring集成Jersey是差不多的。这主要是用spring boot集成Jersey,不细说Jersey。

(很早之前的看的一篇jersey的教程:使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务

(题外话:记得当初在spring中使用jersey的一个问题是,在endpoints中无法注入其他service/dao的bean。然后,貌似记得是通过spring的上下文强制getBean()才把别的service/到注入到了enpoint中。不清楚是那框架搭建有问题,还是怎么的,只是记得遇到过这奇怪的问题。)

【spring boot】SpringBoot初学(5)– WebService之Jersey的更多相关文章

  1. Spring Boot 使用 CXF 调用 WebService 服务

    上一张我们讲到 Spring Boot 开发 WebService 服务,本章研究基于 CXF 调用 WebService.另外本来想写一篇 xfire 作为 client 端来调用 webservi ...

  2. Spring Boot 使用 JAX-WS 调用 WebService 服务

    除了 CXF 我们还可以使用 Spring Boot 自身默认的组件 JAX-WS 来实现 WebService 的调用. 本项目源码 github 下载 1 新建 Spring Boot Maven ...

  3. Spring Boot]SpringBoot四大神器之Actuator

    论文转载自博客: https://blog.csdn.net/Dreamhai/article/details/81077903 https://bigjar.github.io/2018/08/19 ...

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

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

  5. Spring Boot SOAP Webservice例子

    前言 本文将学习如何利用Spring boot快速创建SOAP webservice服务: 虽然目前REST和微服务越来越流行,但是SOAP在某些情况下,仍然有它的用武之地: 在本篇 spring b ...

  6. 在spring boot微服务中使用JWS发布webService

    发布时间:2018-11-22   技术:Java+spring+maven   概述 在springboot微服务中使用JWS发布webService,在服务启动时自动发布webservice接口. ...

  7. Spring Boot 开发 WebService 服务

    WebService 虽然现在大部分互联网企业不太提倡使用,但在以第三方接口为主导的市场,对方来什么接口你还得用什么接口,不可能把接口重写了.例如大部分传统的大型企业都在用 WebService,并且 ...

  8. Spring boot+CXF开发WebService

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

  9. Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件

    1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor ...

  10. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

随机推荐

  1. 不同宿主的iterator不能进行比较

    int main() { string str1, str2; auto it1 = str1.begin(), it2 = str2.begin(); it1 == it2; ; }

  2. 业余无线电A类考试准备笔记

    在线模拟自测地址:https://liunan.github.io/crac/ 共361题,到LK0074 1004/2890行 Key Word: 要合法 要服从管理 Note: 无线电管理 最高法 ...

  3. Generalized end-to-end loss for speaker verification

    论文题目:2018_说话人验证的广义端到端损失 论文代码:https://google.github.io/speaker-id/publications/GE2E/ 地址:https://www.c ...

  4. C标准库与嵌入式

    stddef.h,其中包括size_t,sizeof函数返回值,不同平台的大小不一致 Size and pointer difference types[edit] The C language sp ...

  5. React之拆分组件与组件之间的传值

    父子组件传值: 父组件向子组件传值通过向子组件TodoItem进行属性绑定(content={item}.index={index}),代码如下 getTodoItem () { return thi ...

  6. Element-UI ( Dropdow )下拉菜单组件command传输对象

    通过 :command绑定对象数据,handleCommand方法处理数据 template <div v-for="(item, index) in FlyWarningList&q ...

  7. PWA(Progressive web apps),渐进式 Web 应用

    学习博客:https://www.jianshu.com/p/098af61bbe04 学习博客:https://www.zhihu.com/question/59108831 官方文档:https: ...

  8. 对象级别锁 vs 类级别锁(Java)

    前言 对于多线程(并发)和Spring Boot这两块在同步进行学习中,在看到使用synchronized关键字使操作同步时,看到和C#中不一样的东西,所以这里呢,就深入学习了下,若有错误之处,还望指 ...

  9. ID生成器之——别人家的方案and自家的方案

    “叮咚,叮咚……”,微信提示音一声接一声,声音是那么的频繁,有妖气,待俺去看一看. 这天刚吃完午饭,打开微信,发现我们的技术讨论组里有 100 多条未读消息,心想,是不是系统出问题了?怎么消息那么频繁 ...

  10. JDK SPI 机制

    一.概述 最早看到 SPI 这个机制是在 dubbo 实现 中,最近发现原来也不是什么新东西,竟然就是 JDK 中内置的玩意,今天就来一探究竟,看看它到底是什么玩意! SPI的全称是 Service ...