티스토리 뷰
Spring boot study/8. ETC..
Java Jackson ObjectMapper 를 이용한 Serialize / Deserialize
dudwns3625 2021. 6. 9. 09:51728x90
반응형
SMALL
Java Jackson ObjectMapper
1. pom.xml 에 dependency 추가
- xml 내용
2. Java object to json Serialize<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency>
- Value Object Class
@Data public class TestValue { public String name; public int age; }
- ObjectMapper를 이용해 Object to Json
ObjectMapper objectMapper = new ObjectMapper(); TestValue testValue = new TestValue(); testValue.setAge(30); testValue.setName("name"); //Object to Json // json file로 저장 objectMapper.writeValue(new File("test.json"), testValue); // json String으로 저장 String jsonStr = objectMapper.writeValueAsString(testValue);
- Serialize된 Json 내용
{"name":"name","age":30}
- Json to Java Object
objectMapper.readValue(new File("test.json"), TestValue.class);
728x90
반응형
LIST
'Spring boot study > 8. ETC..' 카테고리의 다른 글
Java JAXB Marshalling / UnMarshalling (0) | 2021.06.09 |
---|---|
Java Gson Library Serialize / Deserialize (0) | 2021.06.09 |
JPA Auditing으로 created, modified 시간 자동화. (0) | 2021.02.04 |
Static변수에 injection하는 방법 (0) | 2021.01.19 |
Spring boot environment를 이용해 외부 설정 주입 (0) | 2021.01.14 |
댓글