일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- mutable
- redis
- Action
- 사이드 프로젝트
- immutable
- Github
- QueryDSL
- compiler
- template
- JUnit
- springboot
- rds
- CodeDeploy
- db
- 토비의 스프링
- AOP
- Spring
- workflow
- JPA
- 알고리즘
- git
- EC2
- kotlin
- aws
- string
- java
- build_test
- Airflow
Archives
- Today
- Total
개발 일기
SpringBoot convert snake to camel, camel to snake 본문
Spring Boot 로 restapi 서버를 만들다보면
USER_NAME 처럼 스네이크 케이스로 오는 json 파라매터를
자바의 카멜 케이스처럼 userName 으로 받고싶은 경우가 있습니다.
이 경우 스프링에서 사용하는 jackson 라이브러리의 설정을 해주면되는데
아래와 같이 설정 할 수 있습니다.
1. 자바 설정
public class NotationConvert {
@Bean
public ObjectMapper jacksonBuilder() {
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder();
jackson2ObjectMapperBuilder.propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
return jackson2ObjectMapperBuilder.build();
}
}
하지만 이 코드는.. @Deprecated
되어 있기 때문에 다음과 같은 방법을 추천드립니다.
application.yml or properties 파일에
spring:
jackson:
property-naming-strategy: SNAKE_CASE
위와 같이 기재하면
@GetMapping("/test-jackson")
@ResponseStatus(HttpStatus.OK)
public MemberTestRequestDTO testJackSon(@RequestBody MemberTestRequestDTO memberTestRequestDTO) {
return MemberTestRequestDTO.builder().memberSnsId("adgf").nickName("홍길동").build();
}
@ToString
@Getter
@RequiredArgsConstructor
public class MemberTestRequestDTO {
private String nickName;
private String memberSnsId;
@Builder
public MemberTestRequestDTO(String nickName, String memberSnsId) {
this.nickName = nickName;
this.memberSnsId = memberSnsId;
}
}
이제 postMan 으로 테스트를 해보겠습니다.
Body를 보면 스네이크 케이스로 변환되는걸 볼 수 있습니다.!
'Spring' 카테고리의 다른 글
SpringBoot 2.5.x , Java11 버전 이상에서 QueryDSL 설정하기 (0) | 2021.08.31 |
---|---|
[JUnit] JUnit , AssertJ , MockMVC 메소드 (0) | 2021.08.16 |
SpringBoot 2.5.X 버전 이상에서 QueryDsl 설정하기 (0) | 2021.08.05 |
SpringBoot 에서 Test DB H2 로 설정하기 (0) | 2021.08.02 |
Github action 과 aws codeDeploy를 이용하여 SpringBoot 프로젝트 ec2에 배포하기 - 2 (0) | 2021.07.25 |