Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 添加用于验证的绑定_Java_Spring_Spring Restcontroller_Spring Rest - Fatal编程技术网

Java 添加用于验证的绑定

Java 添加用于验证的绑定,java,spring,spring-restcontroller,spring-rest,Java,Spring,Spring Restcontroller,Spring Rest,我想创建用于验证Java对象的Spring端点。我尝试实现这个示例: 我试过这个: public class WpfPaymentsDTO { @NotNull @Size(min = 4, max = 15) private String card_holder; private String card_number; .... } 终点: @PostMapping(value = "/payment/{unique_transaction_i

我想创建用于验证Java对象的Spring端点。我尝试实现这个示例:

我试过这个:

public class WpfPaymentsDTO {

    @NotNull
    @Size(min = 4, max = 15)
    private String card_holder;

    private String card_number;
    ....
}
终点:

 @PostMapping(value = "/payment/{unique_transaction_id}", consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
      public ResponseEntity<StringResponseDTO> handleWpfMessage(@PathVariable("unique_transaction_id") String unique_transaction_id,
          @RequestBody WpfPaymentsDTO transaction, BindingResult result, HttpServletRequest request) throws Exception {

        if (result.hasErrors()) {
            List<String> errors = result.getAllErrors().stream()
              .map(DefaultMessageSourceResolvable::getDefaultMessage)
              .collect(Collectors.toList());
            return new ResponseEntity<>(errors, HttpStatus.OK);
        } 

        return ResponseEntity.ok(new StringResponseDTO("test"));
      }
@PostMapping(value=“/payment/{unique\u transaction\u id}”,消费={MediaType.APPLICATION\u JSON\u value},产生={MediaType.APPLICATION\u JSON\u value})
public ResponseEntity handleWpfMessage(@PathVariable(“unique_transaction_id”)字符串unique_transaction_id,
@RequestBody WpfPaymentsDTO事务、BindingResult结果、HttpServletRequest请求)引发异常{
if(result.hasErrors()){
列表错误=result.getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.toList());
返回新的响应状态(错误,HttpStatus.OK);
} 
返回ResponseEntity.ok(新字符串responseTo(“test”));
}
使用表单时,我希望验证所有字段。但目前我遇到了这个错误:
无法推断ResponseEntity的类型参数


实现这一点的正确wya是什么?

尝试使用角度反应形式(FormGroup和FormControl)。 我认为这样更容易。

方法签名中缺少注释。 如果您查看所引用的示例,您将看到它用于
User
对象

因此,在你的情况下:

@Valid @RequestBody WpfPaymentsDTO transaction
此外,您还将在中返回两种不同的类类型

1) 验证成功场景中的响应性

2) 验证失败场景中的响应性

原因如下:

但目前我得到了这个错误:无法推断的类型参数 反应性

如果查看引用的示例,方法返回类型为
ResponseEntity

因此,您的方法应更改为:

  @PostMapping(value = "/payment/{unique_transaction_id}", 
     consumes = { MediaType.APPLICATION_JSON_VALUE }, 
     produces = { MediaType.APPLICATION_JSON_VALUE })
  public ResponseEntity<Object> handleWpfMessage(
                 @PathVariable("unique_transaction_id") String unique_transaction_id,
                 @Valid @RequestBody WpfPaymentsDTO transaction, 
                 BindingResult result, 
                 HttpServletRequest request) throws Exception {
@PostMapping(value=“/payment/{unique\u transaction\u id}”,
使用={MediaType.APPLICATION\u JSON\u VALUE},
产生={MediaType.APPLICATION\u JSON\u VALUE})
公众反应处理信息(
@PathVariable(“唯一事务id”)字符串唯一事务id,
@有效的@RequestBody WPFPaymentsTo事务,
绑定结果结果,
HttpServletRequest(请求)引发异常{
更新: 有没有办法找出验证错误是针对哪个变量的 养大的

是的,您可以获得如下所有字段绑定错误:

List<FieldError> errors = bindingResult.getFieldErrors();
for (FieldError error : errors ) {
    System.out.println ("Validation error in field: " + error.getField() 
                    + "! Validation error message: " + error.getDefaultMessage() 
                    + "! Rejected value:" + error.getRejectedValue());
}
List errors=bindingResult.getFieldErrors();
for(FieldError错误:错误){
System.out.println(“字段中的验证错误:”+error.getField()
+!验证错误消息:“+error.getDefaultMessage()
+“!拒绝的值:”+错误。getRejectedValue());
}

我同意,但出于安全考虑,我需要实现此代码。请帮我一把好吗?方法返回
ResponseEntity
,但当
hasrerrors
为真时,您返回
ResponseEntity
。在您发布的示例中,
ResponseEntity
返回。您可以相应地进行更改,或者更改方法的将类型或在
stringresponsed中将错误信息设置为
对象并返回它。您可以粘贴工作示例吗?显示您对响应的期望值(有/没有错误)我有一个问题:我得到了错误,但我找不到我得到这个错误的变量。有没有办法找到引发验证错误的变量?是的,我找到了它,但有错误。getObjectName()我得到
WPFPaymentsTo
。在上述情况下,我如何得到
卡座
?getField()我将向您返回所附的字段谢谢。我还有一个问题在这里描述: