user414967
user414967

Reputation: 5325

Null check for Request body object

Is it possible to do integration test for null check. I passed null value.

HttpEntity<Employee> entity = new HttpEntity<Employee>(null, headers);
restTemplate.exchange(url, httpMethod, entity, String.class);

I got the below error.

{"timestamp":"2018-10-06T14:33:52.113+0000","status":400,"error":"Bad Request","message":"Required request body is missing:"}

@RestController
public class EmployeeController {

    @PostMapping(value = "/employee/save", produces = "application/json")
    public Employee save(@RequestBody Employee employee){
        if(employee==null){
            throw new RuntimeException("Employee is null");
        }
    }
}

class Employee {

}

Upvotes: 1

Views: 22080

Answers (5)

rohiit9
rohiit9

Reputation: 23

You can use @NonNull either with the @RequestBody annotation. Or, even better if you want the variables of the Object to always be non-null after the constructor is called, you can use this annotation in the class itself when defining the variables/attributes.

Upvotes: 0

Semih Erkaraca
Semih Erkaraca

Reputation: 154

You are getting this error message because @RequestBody annotation has default value and you have to send valid request to your method.

You don't need to null check inside of method for request object because @RequestBody has required field and this field's default value is true. If you check the inside of @RequestBody interface you are going to see what I am saying. Also you can see the below;

enter image description here

If you set @RequestBody(required = false) in method's parameter you are not going to get error message, but you need to check your request object is null or not... If you don't check-out of object is null when you use that object you will get "Null Pointer Excepiton"...

your choise... good luck

Upvotes: 0

GauravRai1512
GauravRai1512

Reputation: 844

You can use javax validation framework to check whether @requestbody is null or not

please use below approach: It'll definitely resolve your concern.

Maven Dependency:
   <dependency>
       <groupId>javax.validation</groupId>
       <artifactId>validation-api</artifactId>
       <version>1.1.0.Final</version>
   </dependency>

@RestController
public class EmployeeController {

     @PostMapping(value = "/employee/save", produces = "application/json")
     public Employee save(@NotNull @RequestBody Employee employee){

     }
}

Upvotes: 0

Huy Nguyen
Huy Nguyen

Reputation: 2061

@RequestBody(required=false) Employee employee

please try with required option in @RequestBody.

The problem here is the mapping in spring mvc.

required

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

@RequestBody Employee employee

Your method only is processed the request if employee is not null. Then it considered mapping correctly and pass request to this method and handle it. So the check null condition will be needless here.

Upvotes: 3

Jacob
Jacob

Reputation: 1916

I am not sure whether it's doable, but I will say it is a bad practice.

According to RFC 7231:

The POST method is used to request that the origin server accept the representation enclosed in the request as data to be processed by the target resource.

Since you have annotated your controller PostMapping, there should be request body to server. I don't see the value of writing integration test for null or empty request body.

If we look into the HTTP request structure,

POST /your_url HTTP/1.1
HOST your_host
ContentType ... ContentLength ...

Body line 1

What's the difference between null and empty?

Upvotes: 0

Related Questions