我已经能够生成带有适当组部分的 spring-configuration-metadata.json 文件,但我的属性列表为空:
{
"groups": [
{
"name": "test-configs",
"type": "com.example.configpropsdemo.TestConfigs",
"sourceType": "com.example.configpropsdemo.TestConfigs"
}
],
"properties": [],
"hints": []
}
Run Code Online (Sandbox Code Playgroud)
我的配置属性类如下所示:
@Validated
@Component
@ConfigurationProperties(prefix = "test-configs")
public class TestConfigs extends ArrayList<TestConfigs.TestConfig> {
@Validated
public static class TestConfig {
@NotBlank
private String configName;
@NotBlank
private String username;
@NotBlank
private String password;
//getters and setters...
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 gradle,我的 build.gradle 如下所示:
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
implementation 'org.springframework.boot:spring-boot-starter-validation'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Run Code Online (Sandbox Code Playgroud)
是什么赋予了?我需要在 TestConfigs 类中添加一些内容吗?
小智 1
需要使用@Setter。这是一个简单的示例(STS4 - SpringBoot - Maven - RestTemplate - 微服务):
客户类别
@Component
@ConfigurationProperties(prefix = "brewery", ignoreUnknownFields = false)
@Setter
public class BreweryClient {
private final RestTemplate restTemplate;
private String apihost;
public final String BEER_PATH_V1 = "/api/v1/beer/";
public BreweryClient(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
public BeerDto getBeerById(UUID id) {
return restTemplate.getForObject(apihost + BEER_PATH_V1 + id.toString(), BeerDto.class);
}
}
Run Code Online (Sandbox Code Playgroud)
测试班
@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class BreweryClientTest {
@Autowired
BreweryClient client;
@Test
public void testGetBeerById() {
BeerDto beer = client.getBeerById(UUID.randomUUID());
assertNotNull(beer);
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序属性
brewery.apihost=http://localhost:8080
Run Code Online (Sandbox Code Playgroud)
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1362 次 |
| 最近记录: |