简体   繁体   English

@Valid Springboot 未向 postman 响应发送消息

[英]@Valid Springboot not sending message to postman response

i'm trying to use javax.validation on my Springboot project and the validations are not going to my postman response.我正在尝试在我的 Springboot 项目上使用javax.validation并且验证不会出现在我的 postman 响应中。

  • My Model:我的 Model:
@Lob
@Type(type = "org.hibernate.type.TextType")
@NotNull
@NotEmpty
@Column(name = "nome")
private String nome;
  • My Controller on the POST requisition:我在 POST 申请中的 Controller:
@PostMapping(value = "/templates", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiModelProperty("Cria um novo template")
public ResponseEntity<TemplateDTO> cadastraTemplate(@RequestBody @Valid TemplateDTO templateDTO, UriComponentsBuilder uriComponentsBuilder) {
        Template template = Template.desconvertId(templateDTO);
        templateRepository.save(template);
        URI uri = uriComponentsBuilder.path("template/{id}").buildAndExpand(template.getId()).toUri();
        return ResponseEntity.created(uri).body(new TemplateDTO(template));
    }
  • The @Valid is working, but is sending the message to my console, not when i send him on postman: @Valid正在工作,但正在将消息发送到我的控制台,而不是当我在 postman 上发送他时:

  • The Console message:控制台消息:

2022-08-24 15:44:13.285 ERROR 1668 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.ages.joinfut.Model.Template] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='não deve estar vazio', propertyPath=nome, rootBeanClass=class com.ages.joinfut.Model.Template, messageTemplate='{javax.validation.constraints.NotEmpty.message}'}
]] with root cause
  • On my Postman response:在我的 Postman 响应中:
{
    "timestamp": "2022-08-24T18:44:13.302+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "path": "/example/templates"
}

Validation is working but why isn't going to response body on postman?验证有效,但为什么不响应 postman 上的正文?

When an exception is thrown, Spring has to determine how to convert a Java exception to an Http response.当抛出异常时,Spring 必须确定如何将 Java 异常转换为 Http 响应。 This is done by the ResponseEntityExceptionHandler .这是由ResponseEntityExceptionHandler完成的。 Your default ResponseEntityExceptionHandler is catching the ConstraintViolationException and converting it to a 500 and eating the exception message.您的默认ResponseEntityExceptionHandler正在捕获ConstraintViolationException并将其转换为 500 并吃掉异常消息。 Here's a guide on setting up custom exception handlers so you can populate the response message with relevant info. 这是设置自定义异常处理程序的指南,以便您可以使用相关信息填充响应消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM