<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.12.5</version>
</dependency>
import com.fasterxml.jackson.annotation.JsonProperty;
public class Person {
@JsonProperty("name")
private String name;
@JsonProperty("age")
private int age;
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class Main {
public static void main(String[] args) throws Exception {
Person person = new Person("John", 25);
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
String yamlString = mapper.writeValueAsString(person);
System.out.println(yamlString);
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class Main {
public static void main(String[] args) throws Exception {
String yamlString = "name: John
age: 25";
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Person person = mapper.readValue(yamlString, Person.class);
System.out.println(person.getName());
System.out.println(person.getAge());
}
}