package com.example.objectmapper;
public class User {
private String name;
private Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.example.objectmapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ObjectMapperApplicationTests {
@Test
void contextLoads() throws JsonProcessingException {
System.out.println("---------------");
// TEXT JSON -> Object
// Object -> TEXT JSON
// controller req json(text) -> object
// response object -> json(text)
var objectMapper = new ObjectMapper();
// object -> text
var user = new User("steve", 10);
var text = objectMapper.writeValueAsString(user);
System.out.println(text);
// text -> object
}
}
package com.example.objectmapper;
public class User {
private String name;
private Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.example.objectmapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ObjectMapperApplicationTests {
@Test
void contextLoads() throws JsonProcessingException {
System.out.println("---------------");
// TEXT JSON -> Object
// Object -> TEXT JSON
// controller req json(text) -> object
// response object -> json(text)
var objectMapper = new ObjectMapper();
// object -> text
// object mapper get method를 활용
var user = new User("steve", 10);
var text = objectMapper.writeValueAsString(user);
System.out.println(text);
// text -> object
var objectUser = objectMapper.readValue(text, User.class);
System.out.println(objectUser);
}
}
package com.example.objectmapper;
public class User {
private String name;
private Integer age;
public User() {
this.name = null;
this.age = null;
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.example.objectmapper;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private String name;
private Integer age;
@JsonProperty("phone_number")
private String phoneNumber;
public User() {
this.name = null;
this.age = null;
this.phoneNumber = null;
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String getPhoneNumber() {
return phoneNumber;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}
}