You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将application.yml属性映射为Jackson JsonNode(Spring)

当然可行!把@ConfigurationProperties里的集合换成Jackson JsonNode完全没问题,而且操作起来很简单。

核心思路

Spring Boot的@ConfigurationProperties绑定机制底层支持Jackson的类型转换,所以直接把List<Customer>替换成JsonNode,Spring就能自动把yml里的数组结构映射成Jackson的ArrayNode(JsonNode的子类)。

具体实现步骤

1. 定义配置属性类

直接把customers的类型改成JsonNode,记得提供getter和setter(或者用Lombok的@Data简化代码):

import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "shop")
public class ShopProperties {
    private String name;
    private JsonNode customers;

    // Getters & Setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public JsonNode getCustomers() {
        return customers;
    }

    public void setCustomers(JsonNode customers) {
        this.customers = customers;
    }
}

2. 启用配置属性

需要告诉Spring要处理这个配置属性类,有两种常用方式:

  • 方式一:在配置类上添加@EnableConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(ShopProperties.class)
public class AppConfig {
}
  • 方式二:直接给ShopProperties类添加@Component注解(适合该类在Spring扫描路径下的场景)

3. 确保Jackson依赖存在

Spring Boot的spring-boot-starter-json(或spring-boot-starter-web)已经包含了Jackson核心依赖,如果你是纯配置项目,需要手动引入:

Maven

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-json</artifactId>
</dependency>

Gradle

implementation 'org.springframework.boot:spring-boot-starter-json'

测试验证

你可以在应用启动后注入ShopProperties,验证绑定是否正确:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class ShopApplication {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ShopApplication.class, args);
        ShopProperties shopProps = context.getBean(ShopProperties.class);

        // 打印店铺名称
        System.out.println("店铺名称:" + shopProps.getName());
        // 格式化打印JsonNode内容
        System.out.println("客户列表JsonNode:\n" + shopProps.getCustomers().toPrettyString());

        // 遍历数组类型的JsonNode
        if (shopProps.getCustomers().isArray()) {
            for (JsonNode customer : shopProps.getCustomers()) {
                String name = customer.get("name").asText();
                int age = customer.get("age").asInt();
                System.out.printf("客户:%s,年龄:%d%n", name, age);
            }
        }
    }
}

运行后你会看到yml里的customers数组已经被正确转换成了Jackson的ArrayNode,你可以像操作普通JsonNode一样读取、遍历里面的内容。

额外说明

  • 这种方式的优势在于:如果yml里的客户结构后续发生变化(比如新增字段),你不需要修改ShopProperties类,直接通过JsonNode读取新字段即可,灵活性很高。
  • JsonNode是Jackson的核心类型,支持各种JSON操作,比如判断节点类型、转换为具体值、遍历子节点等,完全能满足你对配置内容的操作需求。

内容的提问来源于stack exchange,提问作者user6429576

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:21:57