본문 바로가기

Spring

RequestParam

참고: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestparam\

 

요청 파라미터 (uri의 쿼리 파라미터 or form-data) 를 특정 클래스 인스터스로 바인딩해주는 역할.

 

Note that use of @RequestParam is optional (for example, to set its attributes). By default, any argument that is a simple value type (as determined by BeanUtils#isSimpleProperty) and is not resolved by any other argument resolver, is treated as if it were annotated with @RequestParam.

 

@Controller
@RequestMapping("/request_param")
public class RequestParamController {

    @GetMapping("/query")
    @ResponseBody
    public Person bindWithQueryParam(@RequestParam int age,
                                     @RequestParam String name) {
        return new Person(age, name);
    }

    @PostMapping(value = "/form_data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public Person bindWithFormData(@RequestParam(required = false) Integer age,
                                   @RequestParam String name) {
        return new Person(age, name);
    }

    static class Person {
        private Integer age;
        private String name;

        public Person(Integer age, String name) {
            this.age = age;
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

 

bindWithQueryParam 메소드는 아래 요청을 처리할 수 있고,

 

{{your_host}}:{{your_port}}/request_param/query?age=10&name=nano

 

bindWithFormData 메소드는 아래와 같은 요청을 처리할 수 있다.

 

 

'Spring' 카테고리의 다른 글

ViewResolver  (0) 2019.10.06
ResponseEntity  (0) 2019.06.30
RequestMapping  (0) 2019.06.29
HandlerMapping  (0) 2019.06.27
Resource Handler  (0) 2019.06.24