分享一下发现的这个添加一行注解即可生成 JSON API 与 GraphQL 的中间件 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
olOwOlo
V2EX    Java

分享一下发现的这个添加一行注解即可生成 JSON API 与 GraphQL 的中间件

  •  
  •   olOwOlo 2018-07-13 22:20:37 +08:00 3900 次点击
    这是一个创建于 2660 天前的主题,其中的信息可能已经有所发展或是发生改变。

    其实我是想给我给它写的 starter 骗一下星星→_→ illyasviel/elide-spring-boot


    Elide 是一个主要基于 JPA 注释自动构建 JSON API / GraphQL 接口的中间件。只需要给实体对象新增一个注解即可获得 CRUD 的 API,同时也提供了接口让你整合业务逻辑,权限控制,swagger 文档...

    类比的话,或许有点儿类似于 Spring Data REST,不过它做得更多,生成的接口也更丰富...


    引入 starter

    引入 starter 即可完成自动配置(Spring Boot 2)

    <dependency> <groupId>org.illyasviel.elide</groupId> <artifactId>elide-spring-boot-starter</artifactId> <version>1.4.0</version> </dependency> 

    如果你需要 GraphQL 的接口,你需要额外引入

    <dependency> <groupId>com.yahoo.elide</groupId> <artifactId>elide-graphql</artifactId> <version>${elide.version}</version> <!-- 目前 starter 指定版本为 4.2.3 --> </dependency> 

    将实体类暴露为接口

    @Setter @NoArgsConstructor @Table(name = "users") @Entity @Include(rootLevel = true) // <---- 在你实体类上添加一行注解 public class User { private Integer id; private String username; private String password; @Id @GeneratedValue public Integer getId() { return id; } public String getUsername() { return username; } public String getPassword() { return password; } } 

    P.S. 请注意你的 JPA 及下文所述的注解最好放在 get 方法上,Elide 目前没有完全支持位于 field 上的注解。

    OK,完成了!!

    1514707997566079.gif

    你已经拥有了 CRUD 的 API 接口了,现在来试试吧。

    C 创建

    使用 JSON API 创建 user

    var data = { "data": { "type": "user", "attributes": { "username": "test", "password": "test" } } }; fetch('http://localhost:8080/api/user', { method: 'POST', headers: { 'Accept': 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json', }, body: JSON.stringify(data), }) .then(respOnse=> response.ok && response.json()) .then(json => console.log(json)); 

    R 查询

    使用 JSON API 分页 & 过滤 & 指定属性 & 排序查询 user

    fetch(encodeURI("http://localhost:8080/api/user?filter[user]=username=='test'&fields[user]=id,username&sort=-id&page[number]=1&page[size]=3&page[totals]"), { method: 'GET', headers: { 'Accept': 'application/vnd.api+json', }, }) .then(respOnse=> response.ok && response.json()) .then(json => console.log(json)); 

    U 更新

    var data = { "data": { "id": "1", "type": "user", "attributes": { "username": "new name" } } }; fetch('http://localhost:8080/api/user/1', { method: 'PATCH', headers: { 'Accept': 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json', }, body: JSON.stringify(data), }) .then(respOnse=> console.log(response.status === 204 ? 'ok' : 'failure')); 

    D 删除

    fetch('http://localhost:8080/api/user/1', { method: 'DELETE', headers: { 'Accept': 'application/vnd.api+json', }, }) .then(respOnse=> console.log(response.status === 204 ? 'ok' : 'failure')); 

    感兴趣的话更多内容可以查看原文

    我这算不算自来水,能不能领广告费?(~ ̄ ̄)~

    2 条回复    2018-07-27 15:13:37 +08:00
    zhuawadao
        1
    zhuawadao  
       2018-07-27 09:29:12 +08:00
    13 天了,都没有回复,心疼楼主!那我来一个吧
    olOwOlo
        2
    olOwOlo  
    OP
       2018-07-27 15:13:37 +08:00
    @zhuawadao 谢谢捧场(手动狗头
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2682 人在线   最高记录 6679       Select Language div class="sep20">
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 08:41 PVG 16:41 LAX 01:41 JFK 04:41
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86