Spring YAML 文件的引导配置问题

Spring boot configuration problem with YAML files

我有以下 yaml 文件

    proj:
     sms:
      gateway:
        username: d
        password: erer
        signature: e
        url: http://link.somelink.com
      actionKeyMap:
       OTP_GENERATOR: Value1
       CREATE_USER: Value2

我正在尝试将 actionKeyMap/gateway 属性 绑定到 Java 映射中,但它不起作用。

我试过下面的代码

@Component
@ConfigurationProperties(prefix="proj.sms")
public class MessageResolver {

    //@Value("${proj.sms.actionKeyMap}")
    private Map<String, String> actionKeyMap;

ConfigurationProperties 或值注释不起作用。

您可以通过此 link 获取有关如何解决您的问题的信息:

它看起来像:

actionKeyMap: '{
  OTP_GENERATOR: "value1",
  CREATE_USER: "value2"
}'

尝试 :(冒号)而不是 .(至)

@Component
@ConfigurationProperties(prefix="proj:sms")
public class MessageResolver {

    @Value("${proj:sms:actionKeyMap}")
    private Map<String, String> actionKeyMap;

您只需声明 GatewayActionKeyMap class 即可匹配 yml属性。或者您可以在此处阅读 Spring 引导参考,link

@Component
@ConfigurationProperties(prefix="proj.sms")
public class MessageResolver {
    private Gateway gateway;
    private ActionKeyMap actionKeyMap;
    //getter/setter
}

``

public class Gateway {
    private String username;
    private String password;
    private String signature;
    private String url;
    //getter/setter
}

``

public class ActionKeyMap {
    private String OTP_GENERATOR;
    private String CREATE_USER;
    //getter/setter
}

Adding code as discussed in comments

Application.yml

proj:
  sms:
    gateway:
        username: d
        password: erer
        signature: e
        url: http://link.somelink.com
    actionKeyMap:
      OTP_GENERATOR: Value1
      CREATE_USER: Value2

Spring开机Application.java

package hello;

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

@SpringBootApplication
//@EnableConfigurationProperties
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    }
}

MessageResolver.java

package hello;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="proj.sms")
public class MessageResolver {

    //@Value("${proj.sms.actionKeyMap}")
    private Map<String, String> actionKeyMap;

    @Value("${proj.sms.actionKeyMap.OTP_GENERATOR}")
    private String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

    public Map<String, String> getActionKeyMap() {
        return actionKeyMap;
    }

    public void setActionKeyMap(Map<String, String> actionKeyMap) {
        this.actionKeyMap = actionKeyMap;
    }

}

GreetingController.java

package hello;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @Autowired
    MessageResolver messageResolver;

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {

        System.out.println(messageResolver.getTest());
        System.out.println(messageResolver.getActionKeyMap());
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

Greeting.java(如果您尝试构建完整的项目)

package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

URL http://localhost:8080/greeting

来自 GreetingController 的控制台输出(第 22 和 23 行的 sop)

Value1
{OTP_GENERATOR=Value1, CREATE_USER=Value2}