[英]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.我正在使用 spring 构建 Web 服务并遇到以下问题。 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.所以,我的问题是如何知道请求正文中是否提供了 firstParam 和 secondParam 的值。
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. Edit1:抱歉,问题不完整:对于 RequestBody: {paramFirst: first Value} 和 paramSecond 的上述请求值将为空。 So, how would I know whether paramSecond is included in request or not.
那么,我怎么知道 paramSecond 是否包含在请求中。
Edit2: I don't want to validate. Edit2:我不想验证。 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.
因为有两种不同的情况,一种是参数的值被赋予null,另一种是参数不包含在请求中。
You could use the @Valid
annotation like so (pseudo code, didn't test it):您可以像这样使用
@Valid
注释(伪代码,没有测试它):
@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
}
Try using Hibernate Validator ( http://hibernate.org/validator/ ), it's really easy to integrate it with Spring.尝试使用 Hibernate Validator ( http://hibernate.org/validator/ ),它很容易与 Spring 集成。 That way, you'll need to annotate your Dto to enforce validation of required params and then call validate.
这样,您需要对 Dto 进行注释以强制验证所需的参数,然后调用 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
}
You could make firstParam and secondParam type Optional :您可以将firstParam和secondParam类型设为Optional :
ADto class ADto类
public class ADto {
private Optional<String> firstParam;
private Optional<String> secondParam;
// getter setter
}
postSomething method postSomething 方法
@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.请注意,当且仅当请求中不存在firstParam (以及secondParam )时
isPresent()
才会返回 false。 Otherwise, even if the value is set to null, it will return true.否则,即使该值设置为 null,它也会返回 true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.