2016년 12월 25일 일요일

Spring RequestParam Validation

You can check request param validation by using annotation.

first of all, you should add MethodValidationPostProcessor to your application as bean.

@Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        return new MethodValidationPostProcessor();
    }

And then you should add the @Validated annotation to your controller class.


package org.blog.test.controller;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import javax.validation.constraints.Size;

@RestController
@Validated
public class ValidationController {

    @RequestMapping(method = RequestMethod.GET, path = "/validate/{name}")
    public String checkRequestParam(@PathVariable String name, @Size(max = 2) @RequestParam String age) {
        return "success";
    }
}


You can find the exception when you don't keep the constraint.

Mon Dec 26 00:17:35 KST 2016
There was an unexpected error (type=Internal Server Error, status=500).
No message available

댓글 없음 :

댓글 쓰기