Category/SpringBoot
@JsonProperty란?
Corinee
2025. 3. 1. 21:01
728x90
@JsonProperty
@JsonProperty는 Jackson 라이브러리에서 제공하는 어노테이션으로, JSON 데이터와 Java 객체 간 매핑할 필드명을 지정할 때 사용됩니다. Spring Boot에서는 Jackson이 기본적으로 포함되어 있으며, JSON 요청/응답을 처리할 때 사용됩니다.
1. @JsonProperty를 사용하는 이유
- JSON 필드명과 Java 필드명이 다를 때 매핑을 맞춰줌
- JSON 직렬화(객체 → JSON 변환)와 역직렬화(JSON → 객체 변환) 시 특정 필드명을 강제할 수 있음
- Snake_case(예: user_name)와 CamelCase(예: userName) 변환을 쉽게 처리할 수 있음
2. @JsonProperty 사용 예제
예제 1: JSON 필드명이 다를 때 매핑
import com.fasterxml.jackson.annotation.JsonProperty;
public class UserDto {
@JsonProperty("user_name") // JSON에서는 "user_name"이지만 Java에서는 "userName"
private String userName;
@JsonProperty("user_email") // JSON 필드명이 "user_email"이지만 Java에서는 "userEmail"
private String userEmail;
// 기본 생성자 필요 (Jackson에서 역직렬화할 때 사용)
public UserDto() {}
public UserDto(String userName, String userEmail) {
this.userName = userName;
this.userEmail = userEmail;
}
// Getter & Setter
public String getUserName() { return userName; }
public String getUserEmail() { return userEmail; }
}
JSON ↔ Java 변환 결과
Java 객체 → JSON 변환 (직렬화)
ObjectMapper objectMapper = new ObjectMapper();
UserDto user = new UserDto("Alice", "alice@example.com");
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
🔹 출력되는 JSON (필드명이 @JsonProperty로 지정한 이름으로 변환됨)
{
"user_name": "Alice",
"user_email": "alice@example.com"
}
JSON → Java 객체 변환 (역직렬화)
String jsonInput = "{ \"user_name\": \"Bob\", \"user_email\": \"bob@example.com\" }";
UserDto user = objectMapper.readValue(jsonInput, UserDto.class);
System.out.println(user.getUserName()); // Bob
System.out.println(user.getUserEmail()); // bob@example.com
🔹 JSON 키(user_name, user_email)가 Java 필드(userName, userEmail)에 정상적으로 매핑됨
3. @JsonProperty 사용 예제 (Spring Boot)
Spring Boot에서 API 요청/응답 DTO에서 JSON 필드명을 맞추는 경우
import com.fasterxml.jackson.annotation.JsonProperty;
public class LoginRequest {
@JsonProperty("email")
private String userEmail;
@JsonProperty("password")
private String userPassword;
public LoginRequest() {}
public LoginRequest(String userEmail, String userPassword) {
this.userEmail = userEmail;
this.userPassword = userPassword;
}
public String getUserEmail() { return userEmail; }
public String getUserPassword() { return userPassword; }
}
📌 Spring Boot API 요청 예시
{
"email": "test@example.com",
"password": "securepassword"
}
📌 Spring Boot 컨트롤러에서 JSON을 받는 경우
@RestController
@RequestMapping("/api/auth")
public class AuthController {
@PostMapping("/login")
public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest) {
return ResponseEntity.ok("User Email: " + loginRequest.getUserEmail());
}
}
🔹 JSON 요청에서 "email" 필드가 Java의 userEmail 필드에 자동 매핑됨
4. 요약
JSON 필드명 변경 | JSON의 필드명을 Java 필드명과 다르게 설정 |
직렬화 (객체 → JSON 변환) | Java 객체를 JSON으로 변환할 때 필드명을 변경 |
역직렬화 (JSON → 객체 변환) | JSON 데이터를 Java 객체로 변환할 때 필드명을 맞춰줌 |
CamelCase ↔ Snake_case 변환 | "user_name" ↔ userName 변환 가능 |
- @JsonProperty는 JSON 필드와 Java 필드의 매칭을 명확하게 해주는 역할
- CamelCase ↔ Snake_case 변환을 쉽게 처리
- Spring Boot API에서 JSON 요청/응답을 다룰 때 사용
728x90