Spring_Mapping

song·2023년 10월 26일

Spring

목록 보기
7/19

설정

GetMapping, PostMapping을 쓰려면 아래 환경설정을 꼭 해야한다.
그런데 이 두 어노테이션은 스프링 버전 4.3.x 버전부터 사용 가능하며, 이전 버전에서는 사용할 수 없기 때문이다.

poom.xml에 가서 스프링버전을 5.2.0으로 바꾼다.
그리고 poom.xml 우클릭 > maven > update한다.

그러면 이렇게 maven dependencies가보면 5.2.0으로 다 바뀌어있다.

같은 이름으로 mapping하기

  • @RequestMapping("/register/add")을 할 때 같은 이름으로 맵핑을 받으면 에러가 난다. (Ambiguous mapping. error)
  • 그럴 때 @RequestMapping(value = "/register/add", method = RequestMethod.GET), @RequestMapping(value = "/register/add", method = RequestMethod.POST) 로 각각해놓으면 되는데 이것은 옛날 방식이라 잘 사용하지 않는다.
  • @GetMapping("/register/add"), @PostMapping("/register/add") 으로 각각 설정해놓으면 된다.

Get이던 Post던 상관하지 않게 받기

  • @GetMapping, @PostMapping으로 구분해서 받을 수 있다.
    • @RequestMapping(value = "/register/add", method = RequestMethod.GET)
    • @RequestMapping(value = "/register/add", method = RequestMethod.POST)
      위와같이 해놓으면 GET방식인지 POST방식인지 구별해서 맞지 않으면 에러가 뜬다.
  • GET 방식과 POST방식 둘 다 사용하려면 아래와같이 하면 된다.
    • @RequestMapping(value = "/register/add", method = {RequestMethod.GET, RequestMethod.POST})
profile
계속 나아가기

0개의 댓글