【问题标题】:What is the benefit of passing Data Transfer Object as parameter to GET in spring boot?在 Spring Boot 中将数据传输对象作为参数传递给 GET 有什么好处?
【发布时间】:2018-11-28 16:15:22
【问题描述】:

我可以通过以下两个选项通过 DTO,但有人可以解释一下哪个更好以及为什么: 1)。公共列表 getWithRequestParam(@RequestParam(value = "personDTO") String personDTO) 抛出 IOException {public List getWithRequestParam(@RequestParam(value = "personDTO") String personDTO) 抛出 IOException {......}

2)。公共列表 getWithRequestParam(PersonDTO personDTO){ }

【问题讨论】:

    标签: spring spring-boot spring-data-jpa resttemplate dto


    【解决方案1】:

    选项 2 更好,因为它实际上为您提供了 PersonDTO 对象的实例。选项 1 只是给你一个字符串,你必须自己反序列化它才能从中收集任何有意义的东西。

    【讨论】:

    • 谢谢 Iane,这解释了它,但如果我想通过 public List getWithRequestParam(@RequestParam(PersonDTO personDTO) 。
    • 要将其作为请求参数发送,那么它需要是 PersonDTO 的 url 编码的 json 表示。然而,更好的方法是将人员的 id 作为路径变量发送并在您的服务层中查找它。
    • 不,我的要求是发送一组 5 个参数,我必须根据这些参数匹配数据库记录,而不仅仅是单个 ID
    • 在这种情况下,我会将每个参数设为请求参数,例如 a=1&b=2&c=3...因为您正在有效地创建过滤器
    • 取而代之的是,我将所有参数保存在 DTO 中并传递给方法
    猜你喜欢
    • 2018-06-27
    • 2016-11-09
    • 1970-01-01
    • 2021-03-20
    • 2010-11-26
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多