MVC 활용(8) : HTTP 요청 맵핑하기 7부 - 연습 문제
8. HTTP 요청 맵핑하기 7부 - 연습 문제
다음 요청을 처리할 수 있는 핸들러 메소드를 맵핑하는 @RequestMapping (또는 @GetMapping, @PostMapping 등)을 정의하세요.
- GET /events
@Controller
public class SampleController {
@GetMapping("/events")
@ResponseBody
public String events(){
return "events";
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void getEvents() throws Exception{
mockMvc.perform(get("/events"))
.andDo(print())
.andExpect(status().isOk());
}
}
- GET /events/1,
GET /events/2,
GET /events/3,
@Controller
public class SampleController {
@GetMapping("/events/{id}")
@ResponseBody
public String getEvents(@PathVariable int id){
return "events " + id;
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void getEventsWithId() throws Exception{
mockMvc.perform(get("/events/1"))
.andDo(print())
.andExpect(status().isOk());
mockMvc.perform(get("/events/2"))
.andDo(print())
.andExpect(status().isOk());
mockMvc.perform(get("/events/3"))
.andDo(print())
.andExpect(status().isOk());
}
}
- POST /events CONTENT-TYPE: application/json ACCEPT: application/json
@Controller
public class SampleController {
@PostMapping(
value="/events",
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public String createEvent(){
return "event";
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void createEvent() throws Exception{
mockMvc.perform(post("/events")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk());
}
}
- DELETE /events/1,
DELETE /events/2,
DELETE /events/3,
...
@Controller
public class SampleController {
@DeleteMapping("/events/{id}")
@ResponseBody
public String deleteEvents(@PathVariable int id){
return "events " + id;
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void deleteEvents() throws Exception{
mockMvc.perform(delete("/events/1"))
.andDo(print())
.andExpect(status().isOk())
mockMvc.perform(delete("/events/2"))
.andDo(print())
.andExpect(status().isOk());
mockMvc.perform(delete("/events/3"))
.andDo(print())
.andExpect(status().isOk());
}
}
- PUT /events/1 CONTENT-TYPE: application/json ACCEPT: application/json, PUT /events/2 CONTENT-TYPE: application/json ACCEPT: application/json, ...
@Controller
public class SampleController {
@PutMapping(
value="/events/{id}",
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public String updateEvent(@PathVariable int id){
return "events " + id;
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void updateEvent() throws Exception{
mockMvc.perform(put("/events/1")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk());
mockMvc.perform(put("/events/2")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk());
}
}
참고
- 인프런 : 스프링 웹 MVC(백기선)
Author And Source
이 문제에 관하여(MVC 활용(8) : HTTP 요청 맵핑하기 7부 - 연습 문제), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jsj3282/스프링-MVC-활용8-HTTP-요청-맵핑하기-7부-연습-문제저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)