'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 {
}
Solution 1:[1]
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?
Solution 2:[2]
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){
}
}
Solution 3:[3]
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;
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
Solution 4:[4]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jacob |
| Solution 2 | senjin.hajrulahovic |
| Solution 3 | |
| Solution 4 | Jeremy Caney |

