我已经能够生成带有适当分组部分的spring-configuration-metadata.json文件,但是我的属性列表为空:
{
"groups": [
{
"name": "test-configs",
"type": "com.example.configpropsdemo.TestConfigs",
"sourceType": "com.example.configpropsdemo.TestConfigs"
}
],
"properties": [],
"hints": []
}
我的配置属性类如下:
@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...
}
}
我正在使用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'
}
有什么作用?我需要添加一些东西到我的TestConfigs类中吗?
答案 0 :(得分:0)
需要使用@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);
}
}
测试类
@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class BreweryClientTest {
@Autowired
BreweryClient client;
@Test
public void testGetBeerById() {
BeerDto beer = client.getBeerById(UUID.randomUUID());
assertNotNull(beer);
}
}
application.properties
brewery.apihost=http://localhost:8080
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>