Home Mapping
Post
Cancel

Mapping

기존 project에서 내가 원래 작성했던 코드랑 달라 궁금증이 생겨서 검색해보게 되었다.
그리고 코드를 변경하게 되었다.😄
*Notion 참고



Mapping

특정 uri로 요청을 보내면 Controller에서 어떠한 방식으로 처리할지 정의 한다.

이때 들어온 요청을 특정 메서드와 매핑하기 위해 사용하는 것이 @RequestMapping이다.

@RequestMapping에서 가장 많이 사용하는 부분은 valuemethod이다.

*value : 요청받을 url

*method : 어떤 요청으로 받을지 정의

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String helloGet(...) {
        ...
    }

    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public String helloPost(...) {
        ...
    }

    @RequestMapping(value = "/hello", method = RequestMethod.PUT)
    public String helloPut(...) {
        ...
    }

    @RequestMapping(value = "/hello", method = RequestMethod.DELETE)
    public String helloDelete(...) {

⬇️

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@GetMapping()
    public String helloGet(...) {
        ...
    }

    @PostMapping()
    public String helloPost(...) {
        ...
    }

    @PutMapping()
    public String helloPut(...) {
        ...
    }

    @DeleteMapping()
    public String helloDelete(...) {
        ...
    }

@GetMapping, @PostMapping, @PutMapping, @DeleteMapping으로 간단하게 생략 가능하다.



@RequestMapping에서는 다른 옵션을 넣어주는 경우에는 url 앞에 value를 붙여야 한다.

그러나 옵션이 value 하나인 경우에는 그냥 url만 넣어준다.

그렇게 되면서 코드가 점점 길어지니깐 이것을 하나의 어노테이션으로 제공한다고 한다.

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping


Spring MVC - 기능 정리
[Spring] @RequestMapping이란?

This post is licensed under CC BY 4.0 by the author.