반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 필터vs인터셉터
- 필터의 정의
- spring MVC
- 세션의 정의
- MVC
- @Controller
- jpa
- abap value in field Data Class error
- 401오류
- 세션vs쿠키
- 구글 보안 api 활용
- SpringMVC
- Validation
- controller
- 김영한
- filter vs interceptor
- java.lang.AssertionError
- application-properties
- 인터셉터의 정의
- n+1
- 쿠키란
- 세션이란
- Testcode
- BindingResult
- .orelseThrow
- spring
- 쿠키의 정의
- optional
- springSecurityFilterChain 오류
- 유연한 컨트롤러1 - v5
Archives
- Today
- Total
ABAP DUMP ERROR 24시
스프링 서버에서 요청 데이터 만들기 본문
반응형
# 인프런 김영한의 스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술을 개인적으로 정리한 글입니다.
Q . 스프링 서버에서 요청 데이터 만들기
대표적인 요청 3가지는 GET 쿼리 파라미터 , POST-HTML FORM 방식 그리고 HTTP API 방식이 존재한다.
Q. 그래서 내가 해깔리는게 뭐야? 한방정리.
요청 파라미터 방식을 사용하는 것은
GET 쿼리 파라미터 , POST-HTML FORM 방식
@RequestParam , @ModelAttribute를 사용한다.
HTTP MESSAGE BODY를 통해 데이터가 넘어오는 방식은
HTTP API 방식
@RequestBody를 사용한다. -주로 이거 사용 (JSON을 반환해줌)
1. 요청 파라미터 방식 예시
@ResponseBody
@RequestMapping("/request-param-v3")
public String requestParamV3(
@RequestParam("username") String username,
@RequestParam("age") int age){
log.info("username = {}, age={}",username,age);
return "OK";
@ResponseBody
@RequestMapping("/request-param-required")
public String requestParamRequired(
@RequestParam(required = true) String username, // required =true 하면 필수로 username을 가져와야 한다. 없으면 400 오류 발생
@RequestParam(required = false) Integer age){
log.info("username = {}, age={}",username,age);
return "OK";
// 기본형 int 에서는 null 이 사용 금지 ,, 객체형인 Integer 에는 null이 사용 가능하다. 500 오류 발생
}
@ResponseBody
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(
@ModelAttribute HelloData helloData){ // helloProperties 를 찾는다 properties 는 getter , setter와 같은 애들
log.info("username = {} ,age = {}", helloData.getUsername() ,helloData.getAge());
return helloData.getUsername() +"님 안녕하세요";
}
2. HTTP MESSAGE BODY를 통해 데이터가 넘어오는 방식
@ResponseBody
@PostMapping("request-body-string-v4")
public String responseBodyStringV4(@RequestBody String messageBody){ //@RequestBody를 사용하면 Http MessageBody 정보를 편리하게 조회할수 있다.
// 파라미터로 가져오는 @RequestParam 혹은 @ModelAttribute 와는 관계없이 그냥 Body내용만 콱 잡아서 온다.
log.info("messageBody={}", messageBody);
return "ok";
@ResponseBody
@PostMapping("/request-body-json-v5")
public HelloData requestBodyJsonV5(@RequestBody HelloData helloData){
log.info("username = {}, age = {}", helloData.getUsername(), helloData.getAge());
return helloData;
}
반응형
'[WEB]Back-end > Spring MVC' 카테고리의 다른 글
Thylmleaf 간단 용어 정리 (0) | 2022.02.18 |
---|---|
HTTP 메시지 컨버터가 뭐야? (0) | 2022.02.16 |
스프링 서버에서 응답 데이터 만들기 (0) | 2022.02.16 |
Spring Boot의 Handler의 과정 (0) | 2022.02.14 |
@RestController vs @Controller 간단 정리 (0) | 2022.02.13 |
Comments