import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.properties.PropertiesMapper; import com.fasterxml.jackson.dataformat.properties.PropsSchema; import java.io.File; import java.io.IOException; public class PropertiesToObjectExample { public static void main(String[] args) { PropertiesMapper mapper = new PropertiesMapper(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(mapper); try { File propertiesFile = new File("example.properties"); MyConfig config = objectMapper.readValue(propertiesFile, MyConfig.class); System.out.println(config); } catch (IOException e) { e.printStackTrace(); } } static class MyConfig { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "MyConfig{" + "name='" + name + '\'' + ", age=" + age + '}'; } } } <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-properties</artifactId> <version>2.12.4</version> </dependency>


上一篇:
下一篇:
切换中文