简体   繁体   中英

How to check if Request object contains a given parameter

I have a REST endpoint /v1/abc. It is a POST method. In the request body i send an object Test which looks as below.

Class Test {
   Boolean flag = null;
   String name = null;
}

I invoke the endpoint using swagger.

In the request body, i set below.

{
 "name" : "hello"
}

If you notice, I am not sending flag at all. In my API, i want to test if the request object has flag or not. If not present, i throw an exception.

Can you tell me how to achieve this?

I assume that you have a simple POST method like this one:

@PostMapping(path = "/v1/abc")
public void addAbc(@RequestBody Test test) {
    //code
}

So you could just check test.getFlag() != null and thereafter raise an exception or not.

As an alternative you could add @Valid to your method parameter to enable JSR 303 bean validation and make use of @NotNull on your flag field.

Little remark: the keyword class in your example has to be written in lowercase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM