当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot整合jersey的示例代码

SpringBoot整合jersey的示例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

这篇文章主要从以下几个方面来介绍。简单介绍下jersey,springboot,重点介绍如何整合springboot与jersey。

  • 什么是jersey
  • 什么是springboot
  • 为什么要使用springboot+jersey
  • 如何整合springboot与jersey

什么是jersey

阅读官方文档请点击:。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”}。

项目代码地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网