简体   繁体   English

spring如何知道post请求正文中是否提供了参数值?

[英]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 :您可以将firstParamsecondParam类型设为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.

相关问题 Spring MVC:如何在请求浏览器中设置主体参数,以及如何在Spring MVC的控制器中获取该主体参数? - Spring MVC: How to set body parameter in request browser and how to get this body parameters in controller in Spring MVC? 缺少请求正文的 Spring Boot POST 请求? - Spring Boot POST request with missing request body? Spring / Java Web:如何知道请求是表单发布? - Spring/Java web: how can I know a request is a form post? 为翻新POST请求创建JSON主体参数 - Creating a JSON body parameter for retrofit POST request 带有查询参数的 RestTemplate POST 没有请求正文 - RestTemplate POST with query parameter without request body Spring boot 的 Postman Post 请求保存了整个 JSON 正文,而不仅仅是字段值。 我该如何解决? - Postman Post request for Spring boot saves the entire JSON body instead of just the field value. How do I fix this? 如何调用仅接受字符串值作为请求主体参数的端点 - how to call an endpoint that accepts only string value as request body parameter 如何在 POST 请求正文中检索 IDP 发送的中继状态值 - How to retrieve relaystate value sent by IDP in POST request body Spring Controller中的Post方法请求正文 - Request Body for Post Method in Spring Controller Spring GraphQL HttpGraphQlClient - 需要知道确切的请求正文 - Spring GraphQL HttpGraphQlClient - Need to know the exact request body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM