SpringBoot Rest 最佳实践的一些疑问 - V2EX
LLaMA2

SpringBoot Rest 最佳实践的一些疑问

  •  
  •   LLaMA2 Jun 28, 2021 2061 views
    This topic created in 1778 days ago, the information mentioned may be changed or developed.

    不得已要做后端开发,于是按照官方指导使用 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 一些常用的套路,值得细读和探讨的资料。

    叩首!!!

    3 replies    2021-06-30 16:40:46 +08:00
    fkmc
        1
    fkmc  
       Jun 28, 2021
    hiwind
        2
    hiwind  
       Jun 29, 2021
    我选择手动 set null 或者 单独封 DTO 投影还没用过
    试了一下,可以试试 http://127.0.0.1/accounts/10000?projection=accountDTO
    LLaMA2
        3
    LLaMA2  
    OP
       Jun 30, 2021
    @dejavuwind 按照最佳实践的方式,不使用 spring-boot-starter-data-rest 的话,每个 DO 都会对应着一个 DTO,这样可以在 DTO 中隐藏不不必要的细节,且 DTO 做成更符合 VO 的方式,这样 DO 不用添加一些和 DB 无关的注解,类似于 JsonIgnore 、JsonFormat 等,DTO 中的可以加上 Size Pattern 等注解,这样更符合最佳实践,不过加上了 spring-boot-starter-data-rest,一切都变得不可控且不易理解
    About     Help     Advertise     Blog     API     FAQ     Solana     977 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 107ms UTC 22:00 PVG 06:00 LAX 15:00 JFK 18:00
    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