땅지원
땅지원's Personal blog
땅지원
전체 방문자
오늘
어제
  • 전체 (353)
    • Frontend (2)
      • React (2)
    • Backend (90)
      • Java (16)
      • Python (19)
      • Spring (23)
      • Database (21)
      • Troubleshooting (8)
    • DevOps (27)
      • ELK (13)
    • CS (40)
    • OS (2)
      • Linux (2)
    • Algorithm (95)
      • concept (18)
      • Algorithm Problem (77)
    • 인공지능 (25)
      • 인공지능 (12)
      • 연구노트 (13)
    • 수업정리 (35)
      • 임베디드 시스템 (10)
      • 데이터통신 (17)
      • Linux (8)
    • 한국정보통신학회 (5)
      • 학술대회 (4)
      • 논문지 (1)
    • 수상기록 (8)
      • 수상기록 (6)
      • 특허 (2)
    • 삼성 청년 SW 아카데미 (6)
    • 42seoul (12)
    • Toy project (3)
    • 땅's 낙서장 (2)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 20.11.6 BB21플러스 온라인 학술대회
  • 20.10.30 한국정보통신학회 온라인 학술대회

인기 글

태그

  • 이것이 리눅스다 with Rocky Linux9
  • E
  • I
  • ㅗ
  • D

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
땅지원

땅지원's Personal blog

Backend/Troubleshooting

[Error] @RequestBody로 String or Integer를 받으려고 할 때

2023. 4. 18. 09:34
@PostMapping("{id}")
    public void testMethod(@RequestBody String value, @PathVariable long id) {
        System.out.println("value = " + value);
    }

@RequestBody 동작 순서

  1. HTTP Clients 가 request body에 데이터를 담아 request 를 보낸다.
  2. 컨트롤러 메서드에 @RequestBody가 명시되어 있으면 해당 어노테이션이 스프링에게 request body에 담긴 데이터를 도메인 객체로 역직렬화 처리 하라고 알려준다.
    • 추가적으로 자바 Map 으로도 역직렬화 처리를 해준다

 

@RequestBody Integer id  로 받을 수 없는 이유는 이때 MappingJackson2HttpMessageConverter 가 동작하기 때문이고, 이때 내부적으로 Cannot deserialize value of type `java.lang.Integer` from Object value (token `JsonToken.START_OBJECT`) 이러한 에러가 발생하게 됩니다.

 

@RequestBody String id 로하면 에러가 발생하진 않고, StringHttpMessageConverter 가 동작하게 되어 String 객체에 json 형식의 문자열이 그대로 찍히게 됩니다.

 

@RequestBody를 사용하면 원래 자동으로 객체와 매핑이 되는데 String 하나만 보낸다면 '{"value" : "1234"}' 처럼 JSON 형식이 Dictionary한 모습으로 파싱되어서 들어오기 때문에

 

해당 값을 받으려면 Map<String, String> 을 사용해야 한다.

'Backend > Troubleshooting' 카테고리의 다른 글

[Error] Mixed Content: The page at 'https://...' was loaded over HTTPS, but requested an insecure resource  (0) 2023.02.09
[Error][Docker] WSL 2 installation is incomplete.  (0) 2023.01.05
[Error][SpringBoot] Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.  (0) 2022.12.27
[Error][SpringBoot] Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.  (0) 2022.12.27
[Error][Git] src refspec main does not match any  (0) 2022.12.27
    'Backend/Troubleshooting' 카테고리의 다른 글
    • [Error] Mixed Content: The page at 'https://...' was loaded over HTTPS, but requested an insecure resource
    • [Error][Docker] WSL 2 installation is incomplete.
    • [Error][SpringBoot] Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
    • [Error][SpringBoot] Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
    땅지원
    땅지원
    신입 개발자의 우당탕탕 기술 블로그

    티스토리툴바