All Projects → xwjie → Myrestutil

xwjie / Myrestutil

基于springboot的rest调用框架,定义接口然后即可直接注入使用。

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Myrestutil

X Admin
致力于快速开发中小型后台管理系统项目模板(更新中......)
Stars: ✭ 123 (-13.99%)
Mutual labels:  springboot
Xc Spring Cloud Alibaba
spring cloud alibaba(2.2.1最新版)+nacos+dubbo+gateWay+sentinel+rocketmq+(pgsql/mysql8.0的json支持)+ignite集成可用于docker分布式框架+分布式自动化任务+mybatis多数据源+seate+ shardingSphere分布式分库事务解决方案
Stars: ✭ 131 (-8.39%)
Mutual labels:  springboot
Springbootvulexploit
SpringBoot 相关漏洞学习资料,利用方法和技巧合集,黑盒安全评估 check list
Stars: ✭ 3,196 (+2134.97%)
Mutual labels:  springboot
Watchdog Framework Web
🍻 「停止维护」watchdog-framework的Web前端项目,基于Vue+iView-Admin+Vue-Router+Vuex开发
Stars: ✭ 124 (-13.29%)
Mutual labels:  springboot
Mall Coupons Server
淘宝客项目,支持App,微信小程序,QQ小程序
Stars: ✭ 131 (-8.39%)
Mutual labels:  springboot
Echo
🦄 开源社区系统:基于 SpringBoot + MyBatis + MySQL + Redis + Kafka + Elasticsearch + Spring Security + ... 并提供详细的开发文档和配套教程。包含帖子、评论、私信、系统通知、点赞、关注、搜索、用户设置、数据统计等模块。
Stars: ✭ 129 (-9.79%)
Mutual labels:  springboot
Ssm Demo
🍌Spring+SpringMVC+Mybatis+easyUI实现简单的后台管理系统
Stars: ✭ 1,639 (+1046.15%)
Mutual labels:  springboot
Justauth Spring Boot Starter
Spring Boot 集成 JustAuth 的最佳实践~
Stars: ✭ 143 (+0%)
Mutual labels:  springboot
Lamp Util
lamp-util 的前身是 zuihou-commons,在3.0.0版本之后,改名为lamp-util,它是lamp项目的其中一员。 lamp-util 是 lamp-cloud 和 lamp-boot 项目的核心工具包,开发宗旨是打造一套兼顾 SpringBoot 和 SpringCloud 项目的公共工具类。
Stars: ✭ 129 (-9.79%)
Mutual labels:  springboot
Publiccms
现代化java cms,由天津黑核科技有限公司开发,轻松支撑千万数据、千万PV;支持静态化,服务器端包含; 目前已经拥有全球0.0002%的用户,语言支持中、繁、日、英;是一个已走向海外的成熟CMS产品
Stars: ✭ 1,750 (+1123.78%)
Mutual labels:  springboot
Spring Javafx Examples
Example apps for springboot-javafx-support. See
Stars: ✭ 124 (-13.29%)
Mutual labels:  springboot
Smart Sso
springboot SSO 单点登录,OAuth2实现,支持App登录,支持分布式
Stars: ✭ 1,777 (+1142.66%)
Mutual labels:  springboot
Ruoyi Oracle
(RuoYi)官方仓库 基于SpringBoot的权限管理系统 易读易懂、界面简洁美观。 核心技术采用Spring、MyBatis、Shiro没有任何其它重度依赖。直接运行即可用
Stars: ✭ 134 (-6.29%)
Mutual labels:  springboot
Hotel
基于SpringBoot开发的酒店管理系统
Stars: ✭ 123 (-13.99%)
Mutual labels:  springboot
Java Apollo
关于自己的一些学习文档和学习心得都放在这里啦!!!
Stars: ✭ 140 (-2.1%)
Mutual labels:  springboot
Microservice Recruit
基于微服务架构实现的智能招聘系统(用于毕业设计)-前端地址:https://github.com/Clairezyw/recruit
Stars: ✭ 124 (-13.29%)
Mutual labels:  springboot
Seckill Rocketmq
seckill-rocketmq-基于RocketMQ-电商高并发场景实战
Stars: ✭ 133 (-6.99%)
Mutual labels:  springboot
Tropical Fish
Pragmatic 风格的 Java EE 后端开发脚手架,开箱即用。基于 SpringBoot,技术选型采用主流的框架(Mybatis-Plus,Redisson,Xxl-job,Swagger)。项目特点:自定义查询语法, 可以自由组装查询条件查询数据,配合代码生成模块,提高研发效率;自定义 service 方法级别的文档生成规则,在业务方法增加必要的注解,可生成方法调用树,快速把握复杂代码业务逻辑。
Stars: ✭ 142 (-0.7%)
Mutual labels:  springboot
Skill6 Website Backend
java进阶开发,高级版web项目。基于dubbo实现分布式微服务架构,基于spring boot、spring mvc、mybatis、docker、ELK、redis、vue.js、node.js等技术实现的技术分享交流网站。网站名称:技术流,英文名:skill6。主要功能有:登录注册、单点登录、restful设计、文章及评论、代码及资源下载、主题讨论等。持续更新中...
Stars: ✭ 140 (-2.1%)
Mutual labels:  springboot
Pdf Books
📚 PDF 书籍库
Stars: ✭ 134 (-6.29%)
Mutual labels:  springboot

MyRestUtil

给部门写的基于springboot的rest调用框架。

说明

最近部门在搞服务器,系统和系统直接rest调用越来越多。为了简化调用和基于公司的灵活配置编写了本框架。本框架使用jdk的动态代理技术,给出了spring下最方便简洁的使用方法。所有代码都是自主研发,代码简洁明了,一共不到200行,可基于公司特色随意修改。

使用的时候,只需要定义一个接口类,即可直接注入使用。

框架只演示了get请求和支持http basic认证,需要支持post请求和其他认证方式的(如sso),自己寻找TODO标签补全代码即可。

更新记录

2017.10.11 将 RestUtilInit 由普通bean修改为 BeanFactoryPostProcessor ,保证IRequestHandle优先与其他任何bean注册到容器中

解决bean的依赖问题,不需要在接口类中增加 @Lazy 注解。 感谢 李佳明 修改完善。

2017.10.13 增加功能:支持直接在类里面增加方法调用(使用cglib代理实现)

这个特性效果不错,也很符合实际使用场景。之前的场景是调用http接口的时候,需要先定义一个接口,然后注入调用,现在不需要了,直接在普通类中增加方法即可。

如下:业务代码中增加一个调用http接口的get2方法,然后直接调用即可。

@Rest(value = "http://localhost:8081/test")
public abstract class SomeService {

  /**
   * 在类里面增加一个抽象的http接口调用方法
   * @return
   */
  @GET("/get2")
  public abstract ResultBean get2(@Param("key") String key);

  /**
   * 业务代码类中直接调用http接口
   * 
   * @return
   */
  public String doSomething() {
    // 这里是一些业务代码,中间调用了其他系统的http接口。
    return "调用接口返回结果:" + get2("支持直接在类里面注入使用").getData();
  }
}

使用说明

指定RestTemplate

使用RestTemplate处理请求。需要配置RestTemplate。springboot下配置如下:

@Autowired(required = false)
List<ClientHttpRequestInterceptor> interceptors;

@Bean
public RestTemplate restTemplate() {
  System.out.println("-------restTemplate-------");

  RestTemplate restTemplate = new RestTemplate();

  // 设置拦截器,用于http basic的认证等
  restTemplate.setInterceptors(interceptors);

  return restTemplate;
}

如果需要登录,以http basic认证为例,增加以下配置bean即可

@Component
public class HttpBasicRequestInterceptor implements ClientHttpRequestInterceptor {

  @Override
  public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
      throws IOException {

    // TODO 需要得到当前用户
    System.out.println("---------需要得到当前用户,然后设置账号密码-----------");

    String plainCreds = "xiaowenjie:admin";
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);

    String headerValue = new String(base64CredsBytes);

    HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
    requestWrapper.getHeaders().add("Authorization", "Basic " + headerValue);

    return execution.execute(requestWrapper, body);
  }
}

编写接口类

接口上指定 @Rest 配置服务器信息,方法上指定 @GET 配置请求信息。

import cn.xiaowenjie.myrestutil.http.GET;
import cn.xiaowenjie.myrestutil.http.Param;
import cn.xiaowenjie.myrestutil.http.Rest;
import cn.xiaowenjie.retrofitdemo.beans.ResultBean;

@Rest("http://localhost:8081/test")
public interface IRequestDemo {

  @GET
  ResultBean get1();

  @GET("/get2")
  ResultBean getWithKey(@Param("key") String key);

  @GET("/get3")
  ResultBean getWithMultKey(@Param("key1") String key, @Param("key2") String key2);
}

使用

直接在spring框架上注入接口即可使用。

@Autowired
IRequestDemo demo;

public String test() {
  String msg = "<h1>invoke remote rest result</h1>";

  ResultBean get1 = demo.get1();

  msg += "<br/>get1 result=" + get1;

  ResultBean get2 = demo.getWithKey("key-------");

  msg += "<br/>get2 result=" + get2;

  ResultBean get3 = demo.getWithMultKey("key11111", "key22222");

  msg += "<br/>get3 result=" + get3;

  return msg;
}

使用本工程例子

导入2个springboot工程 myrestserver 和 myrestutil。myrestserver端口为8081,用于测试rest服务。分别启动2个应用,然后访问 http://127.0.0.1:8080/

点击链接,会在8080的后台调用8081的rest请求,并把请求接口返回到页面。

工作原理

框架代码在单独的 MyRestUtil\myrestutil\restutil 目录中,主要逻辑都在 RestUtilInit 上,代码非常精简,一看就明白。

  • 使用 org.reflections.Reflections 得到所有配置了 @Rest 的接口列表
  • 根据 @Rest 得到服务器配置信息 RestInfo
  • 使用 Proxy.newProxyInstance 生成接口的代理类,invoke 方法中根据 @GET 得到该方法请求信息 RequestInfo,调用 IRequestHandle 接口处理请求,。
  • 把生成的代理类注入到spring容器中。

如果不使用RestTemplate需要怎么修改?

实现 IRequestHandle , 注释掉默认的 RestTemplateRequestHandle 类即可。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].