不得已要做后端开发,于是按照官方指导使用 SrpingBoot Web + JPA + Rest
- implementation 'org.springframework.boot:spring-boot-starter-web'
- implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
- implementation 'org.springframework.boot:spring-boot-starter-data-rest'
写起来比较愉快,
domain 如下:
@Entity @Getter @Setter public class Account { @Id @Column(nullable = false, updatable = false) @SequenceGenerator( name = "account_sequence", sequenceName = "account_sequence", allocatiOnSize= 1, initialValue = 10000 ) @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "account_sequence" ) private Long id; @Column(nullable = false, unique = true, updatable = false) private String account; @Column(nullable = false) private String passwd; @Column private String salt; @Column private String nonce; @OneToOne @JoinColumn(nullable = false, updatable = false, unique = true) private Member member; // 偷个懒,投影直接下载这个类内部了 @Projection(name = "accountDTO", types = Account.class) public interface AccountDTO { String getAccount(); String getSalt(); } } 仓储接口
@RepositoryRestResource(excerptProjection = Account.AccountDTO.class) public interface AccountRepository extends JpaRepository<Account, Long> {} 此时启动工程,http://127.0.0.1/accounts接口中 account 的属性 passwd 由于投影的缘故没显示 大约是这个样子,这符合我的预期,我很欣慰!
{ "_embedded": { "accounts": [ { "account": "1008610010", "salt": "xxxxx", // 这里的 passwd 和 nonce 都没有了 "_links": { "self": { "href": "http://127.0.0.1/accounts/10000" }, "account": { "href": "http://127.0.0.1/accounts/10000{?projection}", "templated": tru }, "member": { "href": "http://127.0.0.1/accounts/10000/member" } } } ] }, "_links": { "self": { "href": "http://127.0.0.1/accounts" }, "profile": { "href": "http://127.0.0.1/profile/accounts" } }, "page": { "size": 20, "totalElements": 1, "totalPages": 1, "number": 0 } } 此时调用http://127.0.0.1/accounts/10000,结果不该出现的 passwd 和 nonce 冒出了头。我很想跳起来对着他的狗头就是一脚。
{ "account": "1008610010", "passwd": "999999",// <===========================这里 "salt": "salt", "nonce": "nonce",// <===========================这里 "createAt": "2021-06-22T15:01:17.50913+08:00", "modifyAt": "2021-06-22T15:01:24.41396+08:00", "_links": { "self": { "href": "http://127.0.0.1/accounts/10000" }, "account": { "href": "http://127.0.0.1/accounts/10000{?projection}", "templated": true }, "member": { "href": "http://127.0.0.1/accounts/10000/member" } } } 在此,征求广大外出务工人员,授我渔,喂我!提供一些 SrpingBoot JPA REST 一些常用的套路,值得细读和探讨的资料。
叩首!!!
