
元信息处理,这一步的用处是使用此处理器自动生成配置的元信息,在yml、properties文件配置自定义属性时能有自动提示 参考资料: configuration-metadata-annotation-processor
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>效果:

data:
test:
name: testname
age: 22
id-nos:
- 62212200111101A
- 62212200111101B
- 62212200111101C
ext-map:
home-address:
name: home
value: beijing
work-address:
name: work
value: beijing
user-infos:
- userName: zhangsan
age: 18
- userName: lisi
age: 22参数定义及映射
package com.properties.test.config;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.properties.test.vo.UserInfo;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
@Configuration
@ConfigurationProperties(prefix = "data.test")
@Data
@ToString
public class DemoProperties {
private String name;
private Integer age;
private List<String> idNos;
private List<UserInfo> userInfos;
private Map<String,ExtAddress> extMap;
/**
* 重写set方法,将List<Map>转换成目标类型
* 可以使用序列化或者反射技术
* @param userInfosMap
*/
public void setUserInfos(List<Map<String,Object>> userInfosMap){
ObjectMapper mapper = new ObjectMapper();
try {
userInfos = mapper.readValue(mapper.writeValueAsString(userInfosMap),new TypeReference<List<UserInfo>>() {});
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}使用到的属性对象定义
package com.properties.test.config;
import lombok.Data;
import java.io.Serializable;
@Data
public class ExtAddress implements Serializable {
private String name;
private String value;
}package com.properties.test.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserInfo implements Serializable {
String userName;
Integer age;
String tName;
}测试接口编写
package com.properties.test.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.properties.test.config.DemoProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("demo")
public class DemoController {
@Autowired
DemoProperties demoProperties;
@ResponseBody
@GetMapping("/test")
public String test(){
log.info("properties:{}", JSONObject.toJSONString(demoProperties));
return JSONObject.toJSONString(demoProperties);
}
}