
不得已要做后端开发,于是按照官方指导使用 SrpingBoot Web + JPA + 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": true }, "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 一些常用的套路,值得细读和探讨的资料。
叩首!!!
1 fkmc 2021 年 6 月 28 日 |
2 hiwind 2021 年 6 月 29 日 我选择手动 set null 或者 单独封 DTO 投影还没用过 试了一下,可以试试 http://127.0.0.1/accounts/10000?projection=accountDTO |
3 LLaMA2 OP @dejavuwind 按照最佳实践的方式,不使用 spring-boot-starter-data-rest 的话,每个 DO 都会对应着一个 DTO,这样可以在 DTO 中隐藏不不必要的细节,且 DTO 做成更符合 VO 的方式,这样 DO 不用添加一些和 DB 无关的注解,类似于 JsonIgnore 、JsonFormat 等,DTO 中的可以加上 Size Pattern 等注解,这样更符合最佳实践,不过加上了 spring-boot-starter-data-rest,一切都变得不可控且不易理解 |