백기선(인프런 강의)/스프링 웹 MVC
HTTP 요청 매핑하기 - 요청 메소드
레알윙
2020. 9. 25. 11:50
반응형
HTTP Method
- GET
- POST
- PUT
- PATCH
- DELETE
GET 요청
- 클라이언트가 서버의 리소스를 쵸어할 때 사용
- 캐싱할 수 있다(조건적인 GET으로 바뀔 수 있다)
- 브라우저 기록에 남는다
- 북마크 할 수 있다
- 민감함 데이터를 보낼 때 사용하지 않는다(URL에 존재)
- imdempotent
- 먹등성
- 여러번 요청을 하더라도 결과가 달라지지 않는 성질
- 먹등성
POST 요청
- 클라이언트가 서버의 리소스를 수정하거나 새로 만들 때 사용
- 서버에 보내는 데이터를 POST 요청 본문에 담는다
- 캐시할 수 없다
- 브라우저 기록에 남지 않는다
- 북마크 할 수 없다
- 데이터 길이 제한이 없다.
- URI를 보내는 데이터를 처리할 리소스를 지칭
PUT 요청
- URL에 해당하는 데이터를 새로 만들거나 수정할 때 사용
- POST와 다른점은 "URI"에 대한 의미가 다르다
- POST의 URI는 보대는 데이터를 처리할 리소스를 지칭
- PUT의 URI는 보내는 데이터에 해당하는 리소스를 지칭
- Idempotent
PATCH 요청
- PUT과 비슷하지만, 기존 엔티티와 새 데이터의 차이점만 보내는 차이
- Idempotent
DELETE 요청
- URI에 해당하는 리소스를 삭제할 때 사용
- Idempotent
스프링 웹 MVC에서 HTTP method 매핑
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
@GetMapping("/getMapping")
public String getMapping() {
return "getMapping";
}
@PostMapping("/poostMapping")
public String xmlMessage() {
return "poostMapping";
}
@RequestMapping(value = "/getrequestMapping", method = RequestMethod.GET)
public String getrequestMapping() {
return "getrequestMapping";
}
@RequestMapping(value = "/postrequestMapping", method = RequestMethod.POST)
public String postrequestMapping() {
return "postrequestMapping";
}
@RequestMapping(value = "/sumrequestMapping", method = { RequestMethod.POST, RequestMethod.GET })
public String sumrequestMapping() {
return "sumrequestMapping";
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void helloTest() throws Exception{
mockMvc.perform(get("/getMapping"))
.andDo(print())
.andExpect(status().isOk());
mockMvc.perform(post("/poostMapping"))
.andDo(print())
.andExpect(status().isOk());
mockMvc.perform(get("/getrequestMapping"))
.andDo(print())
.andExpect(status().isOk());
mockMvc.perform(post("/postrequestMapping"))
.andDo(print())
.andExpect(status().isOk());
mockMvc.perform(get("/sumrequestMapping"))
.andDo(print())
.andExpect(status().isOk());
}
}
참고
- www.w3schools.com/tags/ref_httpmethods.asp
- tools.ietf.org/html/rfc2616#section-9.3
- tools.ietf.org/html/rfc2068
반응형