Coding Global Background
Coding Global

How to use custom validation with @RequestParam in Spring Boot

Archived 3 years ago
0 messages
1 members
Created 3 years ago
Updated 3 years ago
Open in Discord
D
direct_x_34
Script Kiddie!
I have a problem to detect custom validation in Spring Boot example. I defined some validation including in color validator, text length validator and image size validator.

When I try to send a request with non validation values, it passed successfully instead of throwing an error.

How can I fix it? I also shared my repo shown below


Here is the Controller shown below
    @Validated
    public class QrController {
        
            @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.IMAGE_PNG_VALUE)
            public ResponseEntity<byte[]> createQrCode(@RequestParam String text,
                                                       @RequestParam String size,
                                                       @RequestParam String color,
                                                       @RequestParam String backgroundColor,
                                                       @RequestParam(required = false) MultipartFile imageFile) throws IOException {
            
                    CreateQrRequest request = CreateQrRequest.builder()
                            .text(text)
                            .backgroundColor(backgroundColor)
                            .color(color)
                            .size(size)
                            .build();
            
                    ....
            
            }
        }

Here is the CreateQrRequest shown below
    @Data
    @Builder
    public class CreateQrRequest {
    
        @NotBlank
        @TextLength
        private String text;
    
        @NotBlank
        @ImageSize
        private String size;
    
        @NotBlank
        @Color
        private String color;
    
        @NotBlank
        @Color
        private String backgroundColor;
    }

Here is the repo : https://github.com/Rapter1990/qr-generator-example