有没有办法从 YAML 将值映射到对象列表
Is there a way to mapping values to Object list from YAML
我正在尝试从 YAML 文件创建一个服务器对象列表,正如我在此 post 上看到的那样,但是使用 Micronaut。
我的 YAML 文件有这个配置:
server:
-
name: "Server 1"
flow: "both"
environment: "test"
-
name: "Server 2"
flow: "both"
environment: "production"
我的 POJO 是:
package dev.renansouza.server;
public class Server {
private String name;
private String flow;
private String environment;
public Server() {}
public Server(String name, String flow, String environment) {
this.name = name;
this.flow = flow;
this.environment = environment;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFlow() {
return flow;
}
public void setFlow(String flow) {
this.flow = flow;
}
public String getEnvironment() {
return environment;
}
public void setEnvironment(String environment) {
this.environment = environment;
}
@Override
public String toString() {
return "Servers -> name: " + name + " flow: " + flow + " environment: " + environment;
}
}
我的服务是:
package dev.renansouza.server;
import io.micronaut.context.annotation.ConfigurationProperties;
import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import static dev.renansouza.server.ServerService.PREFIX;
@Singleton
@ConfigurationProperties(PREFIX)
public class ServerService {
public static final String PREFIX = "server";
private List<Server> networkRules = new ArrayList<>();
@PostConstruct
public void init() {
for(Server s : this.getServers()) {
System.out.println(s);
}
}
public List<Server> getServers() {
return this.networkRules;
}
}
我在 System.out.println 行上放置了一个断点并以调试模式启动应用程序,但没有任何反应。
我需要做任何额外的配置吗?
在 https://github.com/jeffbrown/renansouzaproperties 查看项目。
https://github.com/jeffbrown/renansouzaproperties/blob/master/src/main/resources/application.yml
micronaut:
application:
name: renansouzaproperties
server:
Server 1:
flow: both
environment: test
Server 2:
flow: both
environment: production
package renansouzaproperties;
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
@EachProperty("server")
public class Server {
private String name;
private String flow;
private String environment;
public Server(@Parameter String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFlow() {
return flow;
}
public void setFlow(String flow) {
this.flow = flow;
}
public String getEnvironment() {
return environment;
}
public void setEnvironment(String environment) {
this.environment = environment;
}
@Override
public String toString() {
return "Servers -> name: " + name + " flow: " + flow + " environment: " + environment;
}
}
package renansouzaproperties;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.HttpStatus;
import java.util.List;
@Controller("/demo")
public class DemoController {
private List<Server> serverList;
public DemoController(List<Server> serverList) {
this.serverList = serverList;
}
@Get("/")
public HttpStatus index() {
for(Server server: serverList) {
System.out.println(server);
}
return HttpStatus.OK;
}
}
如果您启动应用程序并向 http://localhost:8080/demo 发送请求,您将看到如下所示的输出,表明所需的 Server
实例已创建:
Servers -> name: server 1 flow: both environment: test
Servers -> name: server 2 flow: both environment: production
我知道你的问题明确询问了 yaml 中的列表,这个例子没有使用列表,但从其他评论和你发布的示例代码来看,你似乎并不真的需要列表yaml。如果您确实想要在 yaml 中列出一个列表,也有一种方法可以做到这一点。让我知道这是否真的是您所需要的。
希望对您有所帮助。
我正在尝试从 YAML 文件创建一个服务器对象列表,正如我在此 post 上看到的那样,但是使用 Micronaut。
我的 YAML 文件有这个配置:
server:
-
name: "Server 1"
flow: "both"
environment: "test"
-
name: "Server 2"
flow: "both"
environment: "production"
我的 POJO 是:
package dev.renansouza.server;
public class Server {
private String name;
private String flow;
private String environment;
public Server() {}
public Server(String name, String flow, String environment) {
this.name = name;
this.flow = flow;
this.environment = environment;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFlow() {
return flow;
}
public void setFlow(String flow) {
this.flow = flow;
}
public String getEnvironment() {
return environment;
}
public void setEnvironment(String environment) {
this.environment = environment;
}
@Override
public String toString() {
return "Servers -> name: " + name + " flow: " + flow + " environment: " + environment;
}
}
我的服务是:
package dev.renansouza.server;
import io.micronaut.context.annotation.ConfigurationProperties;
import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import static dev.renansouza.server.ServerService.PREFIX;
@Singleton
@ConfigurationProperties(PREFIX)
public class ServerService {
public static final String PREFIX = "server";
private List<Server> networkRules = new ArrayList<>();
@PostConstruct
public void init() {
for(Server s : this.getServers()) {
System.out.println(s);
}
}
public List<Server> getServers() {
return this.networkRules;
}
}
我在 System.out.println 行上放置了一个断点并以调试模式启动应用程序,但没有任何反应。
我需要做任何额外的配置吗?
在 https://github.com/jeffbrown/renansouzaproperties 查看项目。
https://github.com/jeffbrown/renansouzaproperties/blob/master/src/main/resources/application.yml
micronaut:
application:
name: renansouzaproperties
server:
Server 1:
flow: both
environment: test
Server 2:
flow: both
environment: production
package renansouzaproperties;
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
@EachProperty("server")
public class Server {
private String name;
private String flow;
private String environment;
public Server(@Parameter String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFlow() {
return flow;
}
public void setFlow(String flow) {
this.flow = flow;
}
public String getEnvironment() {
return environment;
}
public void setEnvironment(String environment) {
this.environment = environment;
}
@Override
public String toString() {
return "Servers -> name: " + name + " flow: " + flow + " environment: " + environment;
}
}
package renansouzaproperties;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.HttpStatus;
import java.util.List;
@Controller("/demo")
public class DemoController {
private List<Server> serverList;
public DemoController(List<Server> serverList) {
this.serverList = serverList;
}
@Get("/")
public HttpStatus index() {
for(Server server: serverList) {
System.out.println(server);
}
return HttpStatus.OK;
}
}
如果您启动应用程序并向 http://localhost:8080/demo 发送请求,您将看到如下所示的输出,表明所需的 Server
实例已创建:
Servers -> name: server 1 flow: both environment: test
Servers -> name: server 2 flow: both environment: production
我知道你的问题明确询问了 yaml 中的列表,这个例子没有使用列表,但从其他评论和你发布的示例代码来看,你似乎并不真的需要列表yaml。如果您确实想要在 yaml 中列出一个列表,也有一种方法可以做到这一点。让我知道这是否真的是您所需要的。
希望对您有所帮助。