question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BindException validation built-in support

See original GitHub issue

I would discuss with you if org.springframework.validation.BindException could be a great candidate to be a built-in managed exception.

Indeed when you’re validating query params rather than post body like following

@RequestMapping(path = "/handler-invalid-query-strings", method = GET)
public ResponseEntity<String> validRequestQueryStrings(@Valid final PageRequest pageRequest) {
    return ResponseEntity.ok("done");
}

With PageRequest looks like

@Data
public final class PageRequest {

    @Min(value = 0)
    private int page = 0;

    @Min(value = 1)
    private int size = 50;
}

If you try

@Test
public void invalidRequestQueryParams() throws Exception {
    mvc().perform(request(GET, "http://localhost/api/handler-invalid-query-strings?page=-1"))
         .andExpect(status().isBadRequest());
}

I will fail because 500 status code will be returned after Spring will throw org.springframework.validation.BindException


A bit off-topic, but can explain why:

Spring MethodValidationPostProcessor works great with zalando:problem-spring-web, indeed if you replace previous GetMapping with following:

@RequestMapping(path = "/handler-invalid-query-strings", method = GET)
public ResponseEntity<String> validRequestQueryStrings(@Min(value = 0) @RequestParam(value = "page", defaultValue = "0") int page,
        @Min(value = 1) @RequestParam(value="limit", defaultValue = "50") int limit) {
    return ResponseEntity.ok("done");
}

I will work but since method parameter name is removed after compilation the result looks like:

{  
   "violations":[  
      {  
         "field":"getUsers.arg0",
         "message":"must be greater than or equal to 0"
      }
   ],
   "status":400,
   "type":"https://zalando.github.io/problem/constraint-violation",
   "title":"Constraint Violation"
}

As you can see field is equals to getUsers.arg0 which is no really user-friendly. That why I used to create a intermediate object (DTO?) for validation even for GET parameters.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
whiskeysierracommented, Dec 17, 2016

I could also imagine to put this into the javadoc of the ConstraintViolationAdviceTrait.

0reactions
kakawaitcommented, Dec 15, 2016

@whiskeysierra about doc do you really think that this edge case should be part of README.md. From my point of view README.md should be an overview of the application/library/framework and not be pollute by some special case.

Maybe just I can just create an issue to describe the problem (different from BindException support) and close it, thus using issue tracking like a kind of Q&A?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Validation Exception : BindException - Stack Overflow
As far as I know, it is only BindingResult where the order matters. It must follow the bean that is being validated so...
Read more >
Uses of Package org.springframework.validation
Binder that allows for setting property values on a target object, including support for validation and binding result analysis. Errors. Stores and exposes ......
Read more >
Spring Boot Form Validation Tutorial - CodeJava.net
This Spring Boot tutorial helps you learn to code validation for form fields in a Spring Boot application with JSP/Thymeleaf view.
Read more >
5.5. Input Validation - terasoluna.org
This BindingResult must be specified immediately after the form argument. When not specified immediately, org.springframework.validation.BindException is thrown ...
Read more >
Validating Requests in Spring Boot - Marc Denning
In a Spring Boot application, a lot of validation conveniences are ... I want to highlight is that while there are built in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found