Java 在最新的 Spring v4 中使用带有可选主体的 @RequestBody

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28687616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:38:00  来源:igfitidea点击:

Use @RequestBody with optional body in latest Spring v4

javaspringspring-mvchttprequest

提问by Mike Powell

How do I make body optional in REST API calls when using @RequestBodyannotation in Spring?

@RequestBody在 Spring 中使用注释时,如何在 REST API 调用中使 body 可选?

With Spring's latest version, if you use @RequestBodyannotation, it makes client to send body all the time without making it optional.

在 Spring 的最新版本中,如果你使用@RequestBody注解,它会让客户端一直发送正文,而不是让它成为可选的。

I tried @RequestBody (required=false)but that didn't work & still my request comes as null.

我试过了,@RequestBody (required=false)但这没有用,我的请求仍然为空。

How do I manage to get request converted to proper required object without making body mandatory?

如何在不强制要求主体的情况下将请求转换为正确的必需对象?

For eg:

例如:

@RequestMapping(value="/add/employee", method=RequestMethod.POST)
public void addEmployee(@RequestBody Employee employee){
    // ...
}

Here, I want to add employee using POST but without body. How do I do that? Spring latest version throws error "body missing" if I send empty body...

在这里,我想使用 POST 添加员工但没有正文。我怎么做?如果我发送空的正文,Spring 最新版本会抛出错误“缺少正文”...

采纳答案by Hyman Dorson

@Santosh, I'm not sure which required argument you're referring. Mike already mentioned that he tried using @RequestBody (required=false)and request was still null. May be you can elaborate more & give example.

@Santosh,我不确定您指的是哪个必需的论点。Mike 已经提到他尝试使用@RequestBody (required=false)并且请求仍然为空。也许您可以详细说明并举例说明。

@Mike, probably you can try to have another separate converter that will serve your purpose.

@Mike,也许您可​​以尝试使用另一个单独的转换器来满足您的目的。

Note: I noticed same issue with Spring v4.1.6 & Mike could be using that as he mentioned he is using latest version.

注意:我注意到 Spring v4.1.6 存在同样的问题,Mike 可能会使用它,因为他提到他使用的是最新版本。

回答by Santosh Joshi

I guess you are using spring version above 3.2 as there was a issue with the version. @RequestBody should have a required parameter to allow a request body to be optional

我猜您使用的是 3.2 以上的 spring 版本,因为版本存在问题。@RequestBody 应该有一个必需的参数,以允许请求正文是可选的

Have a look at following link Spring @RequestBody Anotation

看看以下链接 Spring @RequestBody Anotation

@RequestBodyBody takes and argument requiredwhich is true by default. Specifying it to false will help you

@RequestBody正文需要和需要参数,默认情况下为 true。将其指定为 false 将帮助您

public abstract boolean required

Whether body content is required. Default is true, leading to an exception thrownin case there is no body content. Switch this to false if you prefer null to be passed when the body content is null.

需要公共抽象布尔值

是否需要正文内容。 默认为 true导致在没有正文内容的情况下抛出异常。如果您希望在正文内容为 null 时传递 null,请将其切换为 false。

回答by cilf

You can use java.util.Optional:

您可以使用java.util.Optional

@RequestMapping(value="/add/employee", method=RequestMethod.POST)
public void addEmployee(@RequestBody Optional<Employee> employee){
    // ...
}