【spring boot】SpringBoot初学(5)– WebService之Jersey
前言
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的更多相关文章
- Spring Boot 使用 CXF 调用 WebService 服务
上一张我们讲到 Spring Boot 开发 WebService 服务,本章研究基于 CXF 调用 WebService.另外本来想写一篇 xfire 作为 client 端来调用 webservi ...
- Spring Boot 使用 JAX-WS 调用 WebService 服务
除了 CXF 我们还可以使用 Spring Boot 自身默认的组件 JAX-WS 来实现 WebService 的调用. 本项目源码 github 下载 1 新建 Spring Boot Maven ...
- Spring Boot]SpringBoot四大神器之Actuator
论文转载自博客: https://blog.csdn.net/Dreamhai/article/details/81077903 https://bigjar.github.io/2018/08/19 ...
- jersey在 spring boot 添加 packages 扫描路径支持
最近公司内部系统要做数据对接,故使用 jersey 来做 restful webservice 接口设计.由于 spring boot 已经集成 jersey,估计直接导入 spring-boot-s ...
- Spring Boot SOAP Webservice例子
前言 本文将学习如何利用Spring boot快速创建SOAP webservice服务: 虽然目前REST和微服务越来越流行,但是SOAP在某些情况下,仍然有它的用武之地: 在本篇 spring b ...
- 在spring boot微服务中使用JWS发布webService
发布时间:2018-11-22 技术:Java+spring+maven 概述 在springboot微服务中使用JWS发布webService,在服务启动时自动发布webservice接口. ...
- Spring Boot 开发 WebService 服务
WebService 虽然现在大部分互联网企业不太提倡使用,但在以第三方接口为主导的市场,对方来什么接口你还得用什么接口,不可能把接口重写了.例如大部分传统的大型企业都在用 WebService,并且 ...
- Spring boot+CXF开发WebService
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件
1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor ...
- Spring Boot+CXF搭建WebService(转)
概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...
随机推荐
- sqlserver partitition and partition table --- partition show
I can not believe that I had done this about two years Now we know there is totally different betwee ...
- meta 的作用 搜集
Meta标签中的format-detection属性及含义 format-detection翻译成中文的意思是“格式检测”,顾名思义,它是用来检测html里的一些格式的,那关于meta的forma ...
- Go语言实现:【剑指offer】和为S的两个数字
该题目来源于牛客网<剑指offer>专题. 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 对应每个测试案 ...
- 微信小程序面试题总结
A类问题 1 请谈谈微信小程序主要目录和文件的作用? project.config.json 项目配置文件,用得最多的就是配置是否开启https校验: App.js 设置一些全局的基础数据等: App ...
- Centos7.6安装zabbix留纪录
1)查看系统版本 [root@zabbix-s41 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@zabbix-s41 ~ ...
- 使用java基础实现一个简陋的web服务器软件
使用java基础实现一个简陋的web服务器软件 1.写在前面 大学已经过了一年半了,从接触各种web服务器软件已经有一年多了,从大一上最开始折腾Windows电脑自带的IIS开始,上手了自己的第一个静 ...
- IP unnumbered interface,某个接口不编号,某个接口不分配IP地址
OSPFv2中,提到点到点链路可以是unnumbered,不编号,不分配IP地址 12.4.1.1. Describing point-to-point interfaces ...
- Linux压缩归档管理
什么是压缩? 压缩就是用时间换取空间,CPU的时间换取磁盘的空间,下载传输的过程中可以节约带宽. zip/unzip zip支持多平台,支持归档压缩.文件经它压缩后会另外产生具有".zip& ...
- 2.【Spring Cloud Alibaba】实现负载均衡-Ribbon
负载均衡的两种方式 如何实现负载均衡 目前已经实现让A总能找到B,如何实现负载均衡 负载均衡的两种方式 服务器端负载均衡 客户端负载均衡 使用Ribbo实现负载均衡 Ribbon是什么 ==Netfl ...
- FTP服务器配置http访问(配置nginx+ftp服务器)
一.搭建nginx服务器 先安装nginx服务器 # yum install nginx -y 启动nginx服务 # systemctl start nginx 浏览器访问:http://192.1 ...