V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
olOwOlo
V2EX  ›  Java

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

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

    其实我是想给我给它写的 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   ·   我们的愿景   ·   实用小工具   ·   3369 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 13:17 · PVG 21:17 · LAX 06:17 · JFK 09:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.