백기선(인프런 강의)/스프링 웹 MVC

HTTP 요청 매핑하기 - URI 패턴 매핑

레알윙 2020. 9. 25. 18:19
반응형

요청 식별자로 매핑하기

  • @requestMapping은 다음의 패턴을 지원
  • ? : 한글자
    • /author/??? -> /authour/123
  • * : 여러글자
    • /author/* -> /author/jinSeok
  • ** : 여러패스
    • /author/** -> /author/jinSeok/book
@RestController
public class SampleController2 {
	
	@GetMapping({"/hello", "/hi"})
	@ResponseBody
	public String hello() {
		return "hello";
	}
	
	@GetMapping({"/hello?"})
	@ResponseBody
	public String hello2() {
		return "hello";
	}
	
	@GetMapping({"/hello/?"})
	@ResponseBody
	public String hello3() {
		return "hello";
	}
	
	@GetMapping({"/hello/*"})
	@ResponseBody
	public String hello4() {
		return "hello";
	}
	
	@GetMapping({"/hello/**"})
	@ResponseBody
	public String hello5() {
		return "hello";
	}
}

 

@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleController2Test {
	@Autowired
	MockMvc mockMvc;
	

	@Test
	public void helloTest() throws Exception{
		//hello
		mockMvc.perform(get("/hello"))
			.andDo(print())
			.andExpect(status().isOk());
		
		//hello?
		mockMvc.perform(get("/hello1"))
		.andDo(print())
		.andExpect(status().isOk());
		
		//hello/?
		mockMvc.perform(get("/hello/1"))
		.andDo(print())
		.andExpect(status().isOk());
		
		//hello/*
		mockMvc.perform(get("/hello/test"))
		.andDo(print())
		.andExpect(status().isOk());
		
		//hello/**
		mockMvc.perform(get("/hello/test/test/test1"))
		.andDo(print())
		.andExpect(status().isOk());
		
	}	
}

 

클래스에 선언한 RequestMapping과 조합

  • 클래스에 언한 URI 패턴뒤에 이어 붙여서 매핑
@RestController
@RequestMapping("/test")
public class SampleController3 {
	
	@GetMapping("/hello")
	@ResponseBody
	public String hello() {
		return "hello";
	}
	
}

 

@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleController3Test {
	@Autowired
	MockMvc mockMvc;
	
	@Test
	public void helloTest() throws Exception{
		//hello
		mockMvc.perform(get("/test/hello"))
			.andDo(print())
			.andExpect(status().isOk());
	}	
}

 

 

정규 표현식으로도 매핑 가능

  • /{name : 정규식}
@RestController
public class SampleController4 {
	
	@RequestMapping("/{name : [a-z]+")
	@ResponseBody
	public String hello(@PathVariable String name) {
		return "hello " + name;
	}
	
}

 

@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleController4Test {
	@Autowired
	MockMvc mockMvc;
	
	@Test
	public void helloTest() throws Exception{
		//hello
		mockMvc.perform(get("/hello/jinseok"))
			.andDo(print())
			.andExpect(status().isOk())
			.andExpect(content().string("hello jinseok"));
	}	
}

 

패턴이 중복되는 경우

  • 가장 구체적으로 맵핑되는 핸들러를 선택
    • 아래같은 경우는 /test/hello를 매핑
@RestController
@RequestMapping("/test")
public class SampleController3 {
	
	@GetMapping("/hello")
	@ResponseBody
	public String hello() {
		return "hello";
	}
	
	@GetMapping("/**")
	@ResponseBody
	public String hello2() {
		return "hello";
	}	
}

 

@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleController3Test {
	@Autowired
	MockMvc mockMvc;
	
	@Test
	public void helloTest() throws Exception{
		//hello
		mockMvc.perform(get("/test/hello"))
			.andDo(print())
			.andExpect(status().isOk())
			.andExpect(content().string("hello jinseok"));
	}	
}

 

URI 확장자 맵핑 지원

  • 이기능을 권장하지 않음(스프링 부트에는 깁노적으로 이기능을 사용하지 않도록 설정)
    • 보안이슈
    • URI 변수, Path매개변수 URI인코딩을 사용할 때 불명확 함

 

 

 

반응형