【问题标题】:Validate empty request验证空请求
【发布时间】:2018-04-02 11:37:54
【问题描述】:

我正在开发一个 SpringBoot 应用程序。对于 POST 端点,没有要传递的请求正文。我的服务工作正常。但是当我在请求正文中传递一些值时它也可以工作。 如果在请求正文中传递了一些内容,理想情况下该请求应为空,我如何验证并返回 BAD REQUEST?

*@RequestMapping(value = "/sample/customers", method = POST)
public Customer session() {
    return customerService.getCustomer();
}*

【问题讨论】:

  • 为什么在没有正文的情况下使用 POST?为什么不 GET?
  • 也许你可以使用@Null String body 作为参数

标签: spring spring-boot validate-request


【解决方案1】:

您不应使用 POST 进行数据检索。

您需要两种方法。这是保存的示例代码:

@ResponseStatus(value = HttpStatus.CREATED)
@RequestMapping(value = "/sample/customers", method = POST)
public Customer save(@RequestBody CustomerRequestDto customerRequestDto) {
    return customerService.save(customerRequestDto);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2018-10-18
    • 2019-04-10
    • 2017-04-09
    • 2017-11-21
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多