SpringBoot整合jersey的示例代码
作者:Angela_Feng 发布时间:2021-08-28 05:23:12
这篇文章主要从以下几个方面来介绍。简单介绍下jersey,springboot,重点介绍如何整合springboot与jersey。
什么是jersey
什么是springboot
为什么要使用springboot+jersey
如何整合springboot与jersey
什么是jersey
阅读官方文档请点击:jsersey。RESTful Web Services in Java即java中的一种restful框架。jersey使用了JAX-RS规范来约束API的开发。既然jersey是基于restful风格的框架,那么什么是restful呢,主要有以下几点:
在rest认为,一切都可以被称为资源。
每个资源都由uri标识。要访问这个资源,必须通过对应的uri去访问。
访问资源使用POST,GET,PUT,DELETE。POST为新增接口,GET为获取接口,PUT为修改接口,DELETE为删除接口。
通过XML/JSON去通信
每次请求都是独立的。
什么是springboot
简单介绍一下,Springboot是由spring衍生的一个框架,boot是轻量的意思,即轻量级的spring。Springboot继承了spring的特性,但是呢,觉得spring太繁琐,于是springboot就简化了spring的配置,不需要写复杂的配置文件就可以实现spring原有的功能特点。只需要在pom.xml中引入依赖就能实现各种模块和技术的整合。
为什么要使用springboot+jersey
如果要实现rest,jersey是一个很不错的选择。springboot是java中一个轻量级的框架,能简化配置,不复杂且功能齐全,因此结合起来使用,也是一个不错的选择。
如何整合springboot与jersey
1.创建maven项目
2.添加springboot配置。
(1)在pom.xml中添加springboot父依赖
<!-- Spring Boot 父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
(2)在pom.xml中添加springbootweb依赖和junit单元测试依赖(如不使用单元测试,可不加),引入依赖后在控制台执行命令 mvn clean install
<dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
(3)创建Springboot入口:Application.java,此时一个springboot的maven项目已经创建成功,执行main函数就可以启动项目。(是不是确实很轻量级..?)
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by Angela on 2017/4/20.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args){
//springboot 入口
SpringApplication.run(Application.class,args);
}
}
(4)添加jersey依赖,在pom.xml中添加依赖,在控制台执行命令mvn install
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
(5)创建jersey配置文件
package com.demo.config.jersey;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
/**
* Created by Angela on 2017/4/20.
*/
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
//构造函数,在这里注册需要使用的内容,(过滤器, * ,API等)
}
}
此时,基于jersey的springboot项目已经搭建成功。我们写demo来验证一下。
(6)基于jersey的api使用
配置文件:
创建项目的配置文件application.yml,指定name为local,端口号为8081,如下:
spring:
name: local
server:
port: 8081
资源,即API,这里以get方法为例:
package com.demo.web;
import com.demo.model.City;
import org.springframework.stereotype.Component;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Created by Angela on 2017/4/20.
*/
@Component
@Path("/demo")
public class Demo {
//path注解指定路径,get注解指定访问方式,produces注解指定了返回值类型,这里返回JSON
@Path("/city")
@GET
@Produces(MediaType.APPLICATION_JSON)
public City get(){
City city = new City();
city.setId(1L);
city.setCityName("beijing");
city.setCityCode("001");
System.out.println(city.toString());
return city;
}
}
jersey配置(有两种注册方式,注册类,注册包):
package com.demo.config.jersey;
import com.demo.web.Demo;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
/**
* Created by Angela on 2017/4/20.
*/
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
//注册类的方式
// register(Demo.class);
//注册包的方式
packages("com.demo.web");
}
}
这里有个小坑。项目打为jar包启动时,不能使用包注册的方式,否则会报FileNotFound异常。
此时,demo已经完成,我们可以通过浏览器或其他工具访问接口,访问路径:http://localhost:8081/demo/city,返回JSON字符串:{“id”:1,”cityName”:”beijing”,”cityCode”:”001”}。
项目代码地址:https://github.com/fengqing0216/learning.git
来源:http://blog.csdn.net/github_38395241/article/details/70265379


猜你喜欢
- 添加POM依赖:<!-- spring-boot-监控--><dependency> <group
- 我们在使用C# 语言的Assembly.Load 来加载托管程序集并使用反射功能时,一般需要先通过Assembly.Load()
- 本文实例讲述了Java 8新增的方法参数反射。分享给大家供大家参考,具体如下:一 点睛Java 8在java.lang.reflect包下新
- Java作为一面向对象的语言,具备面向对象的三大特征——继承,多态,封装。继承顾名思义,继任,承接,传承的意思。面向对象的语言有一个好处,就
- Springboot内部提供的事务管理器是根据autoconfigure来进行决定的。比如当使用jpa的时候,也就是pom中加入了sprin
- Android系统里面有四种类型的菜单:options menu(选项菜单),context menu(上下文菜单),sub m
- 先看MVC模式流程图(其实MVC设计模式就是java中的model2。): &nb
- 文章描述这个程序也记不清是什么时候写的了,犹记得那时我还很年轻,偶然从网上看到了这样一个类似的标题(AI五子棋的实现),进去后看到那个是ja
- 一、TkMybatisTkmybatis 是基于 Mybatis 框架开发的一个工具,通过调用它提供的方法实现对单表的数据操作,不需要写任何
- 本篇随笔主要介绍用Java实现简单的装饰器设计模式:先来看一下装饰器设计模式的类图:从图中可以看到,我们可以装饰Component接口的任何
- 首先,引入依赖:<dependency> <groupId>org.springframe
- 本文实例讲述了Android编程自定义菜单实现方法。分享给大家供大家参考,具体如下:在android开发的过程中系统自带的菜单往往满足不了开
- Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。建议大家使用S
- 本文介绍了C# 用什么方法将BitConverter.ToString产生字符串再转换回去,分享给大家,具体如下:byte[]
- 本文实例讲述了从C#程序中调用非受管DLLs的方法。分享给大家供大家参考。具体方法如下:前言:从所周知,.NET已经渐渐成为一种技术时尚,那
- 废话不多说,直接奉上代码:Frame.javapackage snake;import java.awt.Graphics;import j
- Android程序调用本机googlemap,传递起始和终点位置,生成路线图if (wodeweizhiPoint != null) { i
- 本文实例讲述了Android开发之开关按钮控件ToggleButton简单用法。分享给大家供大家参考,具体如下:先来看看运行效果:具体代码如
- 本文实例讲述了C#求n个数中最大值和最小值的方法。分享给大家供大家参考。具体实现方法如下:using System; using Syste
- 前言空间分配要点有:一是空间分配的连续性;二是动态内存申请;三是防止程序执行中出现异常错误。提示:开始讲解了嗷~后续会根据精力持续更新嗷!!