http://localhost:8080/request-param?username=hello&age=20
POST /request-param ...
content-type: application/x-www-form-urlencoded
username=hello&age=20
@ResponseBody
@RequestMapping("/request-param-default")
public String requestParamDefault(
// username이 필수로 넘어와야 함
@RequestParam(required = true, defaultValue = "guest") String username,
// age는 넘어오지 않아도 됨
@RequestParam(required = false, defaultValue = "-1") int age) {
log.info("username ={}, age ={}", username, age);
return "ok";
}
// 모든 파라미터 값들을 맵의 형태로 가져옴
// 하나의 파라미터 키에 대한 값이 1개인게 확실하다면 Map을 사용해도 되지만, 여러개일 가능성이 있을 경우
// MultiValueMap을 사용하자!!!
@ResponseBody
@RequestMapping("/request-param-map")
public String requestParamMap(@RequestParam Map<String, Object> paramMap) {
log.info("username ={}, age ={}", paramMap.get("username"), paramMap.get("age"));
return "ok";
}
@ResponseBody
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(@ModelAttribute HelloData helloData) {
log.info("username ={}, age ={}", helloData.getUsername(), helloData.getAge());
return "ok";
}
@Slf4j
@Controller
public class RequestBodyStringController {
@PostMapping("/request-body-string-v1")
public void requestBodyStringV1(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
log.info("messageBody={}", messageBody);
response.getWriter().write("ok");
}
@PostMapping("/request-body-string-v2")
public void requestBodyStringV2(InputStream inputStream, Writer responseWriter) throws IOException {
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
log.info("messageBody={}", messageBody);
responseWriter.write("ok");
}
@PostMapping("/request-body-string-v3")
public HttpEntity<String> requestBodyStringV3(HttpEntity<String> httpEntity) throws IOException {
String messageBody = httpEntity.getBody();
log.info("messageBody={}", messageBody);
// ctrl + p : 메소드 매개변수 확인하기
return new HttpEntity<>("ok");
}
@ResponseBody
@PostMapping("/request-body-string-v4")
public String requestBodyStringV4(@RequestBody String messageBody, @RequestHeader Map<String, Object> headers) throws IOException {
for (String key : headers.keySet()) {
log.info("header key={} , value={}",key, headers.get(key));
}
log.info("messageBody={}", messageBody);
return "ok";
}
}
@Slf4j
@Controller
public class RequestBodyJsonController {
private ObjectMapper objectMapper = new ObjectMapper();
// 1. inputStream 이용
@PostMapping("/request-body-json-v1")
public void requestBodyJsonV1(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
log.info("messageBody={}", messageBody);
HelloData helloData = objectMapper.readValue(messageBody, HelloData.class);
log.info("username={}, age={}",helloData.getUsername(), helloData.getAge());
response.getWriter().write("ok");
}
// 2. ObjectMapper 이용
@ResponseBody
@PostMapping("/request-body-json-v2")
public String requestBodyJsonV2(@RequestBody String messageBody) throws IOException {
log.info("messageBody={}", messageBody);
HelloData helloData = objectMapper.readValue(messageBody, HelloData.class);
log.info("username={}, age={}",helloData.getUsername(), helloData.getAge());
return "ok";
}
// 3. @RequestBody로 객체와 자동 매핑
// Http body로 직접 데이터를 보내올경우 @RequestBody 생략 불가능
// 생략할 경우 @ModelAttribute가 적용이 되어 버린다.
@ResponseBody
@PostMapping("/request-body-json-v3")
public String requestBodyJsonV3(@RequestBody HelloData helloData){
log.info("username={}, age={}",helloData.getUsername(), helloData.getAge());
return "ok";
}
// 4. HttpEntity 활용
@ResponseBody
@PostMapping("/request-body-json-v4")
public String requestBodyJsonV4(HttpEntity<HelloData> data){
HelloData helloData = data.getBody();
log.info("username={}, age={}",helloData.getUsername(), helloData.getAge());
return "ok";
}
/** 5. @RequestBody로 객체와 매핑하고, 객체를 이용해 리턴
* @RequestBody 생략 불가능(@ModelAttribute 가 적용되어 버림)
* HttpMessageConverter 사용 -> MappingJackson2HttpMessageConverter (contenttype:
application/json)
*
* @ResponseBody 적용
* - 메시지 바디 정보 직접 반환(view 조회X)
* - HttpMessageConverter 사용 -> MappingJackson2HttpMessageConverter 적용
(Accept: application/json)
*/
@ResponseBody
@PostMapping("/request-body-json-v5")
public HelloData requestBodyJsonV5(@RequestBody HelloData helloData){
log.info("username={}, age={}",helloData.getUsername(), helloData.getAge());
return helloData;
}
}
Inpustream 활용
Http body를 이용한 데이터 전달 방법도 마찬가지로 Inputstream을 이용해 데이터를 가져올 수 있다.
@RequestBody + ObjectMapper
HelloData helloData = objectMapper.readValue(messageBody, HelloData.class);
@RequestBody를 이용해 json형태의 데이터를 String데이터로 가져오고, 이 데이터를 ObjectMapping을 활용하여 객체로 변환할 수 있다.
@RequsetBody
@RequsetBody를 명시하고 객체 타입을 지정해주면 json데이터와 객체의 파라미터를 매핑하여 값을 초기화 해준다.
- 여기서는 @RequestBody를 생략할 수 없는데, 생략해버리면 @ModelAttribute가 적용되어 값이 매핑되지 않는다.
- @ModelAttribute가 적용될 경우에 참조 데이터 타입은 null로, 기본 데이터 타입은 0으로 초기화 된다.
HttpEntity활용
HttpEntity를 이용하여 json데이터를 가져올 수 있다. getBody()로 http body데이터를 가져오고, getHeaders()로 헤더 정보를 가져올 수 있다.
@RequsetBody + 객체 타입 리턴
@RequsetBody를 이용해 객체와 매핑하여 json데이터를 가져오고, 마찬가지로 리턴 값을 객체로 지정하면 객체를 이용해 json데이터를 리턴해준다.