'How to know if a parameter value is provided in post request body in spring?
I am building web service with spring and come across with following problem. There is a post service as follow.
@RequestMapping(value = "/postSomething", method = RequestMethod.POST)
public ResponseDTO postSomething(@RequestBody ADto aDto){
//post data
//return response
}
public class ADto{
private String firstParam;
private String secondParam;
// getter setter
}
So, my question is how can I know whether value of firstParam and secondParam is provided in request body or not.
RequestBody: { paramFirst: null, paramSecond: null}
Edit1: Sorry for incomplete question: For RequestBody: {paramFirst: first Value} and for above request value of paramSecond will be null. So, how would I know whether paramSecond is included in request or not.
Edit2: I don't want to validate. What I want to know is whether request contains a particular parameter or not. Because there are two different cases, one is value of a parameter is given null and other is paramter is not included in request.
Solution 1:[1]
You could use the @Valid
annotation like so (pseudo code, didn't test it):
@RequestMapping(value = "/postSomething", method = RequestMethod.POST)
public ResponseDTO postSomething(@Valid @RequestBody ADto aDto){
// MethodArgumentNotValidException will be thrown if validation fails.
}
You'll need an exception handler to handle the validation error.
@ExceptionHandler
@ResponseBody
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public Error handleException(MethodArgumentNotValidException exception) {
//do something with the validation message: exception.getBindingResult()
}
And your class.
public class ADto{
@NotNull(message = "First parameter can not be null")
private String firstParam;
@NotNull(message = "Second parameter can not be null")
private String secondParam;
// getter setter
}
Solution 2:[2]
Try using Hibernate Validator (http://hibernate.org/validator/), it's really easy to integrate it with Spring. That way, you'll need to annotate your Dto to enforce validation of required params and then call validate.
public class ADto{
@NotNull
private String firstParam;
@NotNull
private String secondParam;
// getter setter
}
@RequestMapping(value = "/postSomething", method = RequestMethod.POST)
public ResponseDTO postSomething(@RequestBody ADto aDto){
validator.validate(aDto)
//post data
//return response
}
Solution 3:[3]
You could make firstParam and secondParam type Optional:
ADto class
public class ADto {
private Optional<String> firstParam;
private Optional<String> secondParam;
// getter setter
}
postSomething method
@RequestMapping(value = "/postSomething", method = RequestMethod.POST)
public ResponseDTO postSomething(@RequestBody ADto aDto) {
if (Optional.ofNullable(aDto.getFirstParam()).isPresent()) {
// firstParam is provided in the request
} else {
// firstParam is not provided in the request
}
if (Optional.ofNullable(aDto.getSecondParam()).isPresent()) {
// secondtParam is provided in the request
} else {
// secondtParam is not provided in the request
}
}
Note that isPresent()
will return false if and only if firstParam (as well as for secondParam) is not present in the request. Otherwise, even if the value is set to null, it will return true.
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 | |
Solution 2 | Gervasio Amy |
Solution 3 | AddeusExMachina |