目录
1.1Yaml配置文件读取
1.2加载yaml文件
1.3工具类
1.4writeValue使用
1.5map遍历工具
依赖导入:
com.fasterxml.jackson.dataformat
jackson-dataformat-yaml
2.9.8
public static WeworkConfig load(String path) {
// fixed: read from yaml or json
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
WeworkConfig weworkConfig = mapper.readValue(WeworkConfig.class.getResourceAsStream(path),WeworkConfig.class);
return weworkConfig;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
备注:
yaml文件位置:src/main/resources/conf/WeworkConfig.yamlWeworkConfig.yam
-----
agentId: "1000005"
secret: "1JPyY9GvPLZfpvxEDjok-Xt_9v7HIBYJhZUoO6EgNGY"
corpid: "wwd6da61649bd66fea"
contactSecret: "C7uGOrNyxWWzwBsUyWEbLQdOqoWPz4hNvxj9RIFv-4U"
current: dev
env:
dev:
"qyapi.weixin.qq.com": "qyapi.weixin.qq.com"
test:
"qyapi.weixin.qq.com": "140.207.127.79"
--------’
单例模式:
private static WeworkConfig weworkConfig;
public static WeworkConfig getInstance(){
if(weworkConfig==null){
weworkConfig=load("/conf/WeworkConfig.yaml");
System.out.println(weworkConfig);
System.out.println(weworkConfig.agentId);
}
return weworkConfig;
}
这里介绍下writeValue和writeValueAsString 将读取的yaml转换成json写进文本
public static void writeAsString() {
ObjectMapper mapper = new ObjectMapper();
WeworkConfig weworkConfig = WeworkConfig.getInstance();
BufferedWriter writer = null;
FileWriter fw=null;
try {
System.out.println(mapper.writeValueAsString(weworkConfig));
File file = new File("src/main/resources/weworkconfig.txt");
if (!file.exists()) {
file.createNewFile();
}
// String jsonData = JSON.toJSONString(weworkConfig);
String jsonData=mapper.writeValueAsString(weworkConfig);
Map map = new LinkedHashMap();
map = mapper.readValue(jsonData, Map.class);
JSONObject jbJsonObject = new JSONObject(map);
String jsonObject = JSON.toJSONString(jbJsonObject, SerializerFeature.PrettyFormat);
fw = new FileWriter(file);
writer = new BufferedWriter(fw);
writer.write(jsonObject);
writer.flush();
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null ||fw!=null) {
try {
writer.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
上面代码使用到了JSONUtil工具,将object转json,json转map,并在将jsonObject转json时使用SerializerFeature.PrettyFormat
可以将json子符传序列化,并使用BufferedWriter字符输出流输出,最终输出到文本的是整齐的json字符串,如下:
weworkconfig.txt文件
{
"agentId":"1000005",
"current":"dev",
"contactSecret":"C7uGOrNyxWWzwBsUyWEbLQdOqoWPz4hNvxj9RIFv-4U",
"corpid":"wwd6da61649bd66fea",
"secret":"1JPyY9GvPLZfpvxEDjok-Xt_9v7HIBYJhZUoO6EgNGY",
"env":{
"dev":{
"qyapi.weixin.qq.com":"qyapi.weixin.qq.com"
},
"test":{
"qyapi.weixin.qq.com":"140.207.127.79"
}
}
}
public static void writeVlueFIle() {
ObjectMapper mapper = new ObjectMapper();
WeworkConfig weworkConfig = WeworkConfig.getInstance();
File file = new File("src/main/resources/weworkconfig.txt");
try {
if (!file.exists()) {
file.createNewFile();
}
mapper.writeValue(file, weworkConfig);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeVlueOutput() {
ObjectMapper mapper = new ObjectMapper();
WeworkConfig weworkConfig = WeworkConfig.getInstance();
File file = new File("src/main/resources/weworkconfig.txt");
OutputStream outputStream=null;
try {
if (!file.exists()) {
file.createNewFile();
}
outputStream=new FileOutputStream(file);
mapper.writeValue(outputStream, weworkConfig);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (outputStream!=null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void writeVlueFileWrite() {
ObjectMapper mapper = new ObjectMapper();
WeworkConfig weworkConfig = WeworkConfig.getInstance();
File file = new File("src/main/resources/weworkconfig.txt");
FileWriter fileWriter=null;
try {
if (!file.exists()) {
file.createNewFile();
}
fileWriter=new FileWriter(file);
mapper.writeValue(fileWriter, weworkConfig);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileWriter!=null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这里写一个map读取遍历方法:
public void MapRead(Map map) {
/* Set> entrys =map.entrySet();
for (Entry> entry : entrys) {
System.err.println("key:" + entry.getKey() + " value:" + entry.getValue());
}*/
Set> entrys = map.entrySet();
for (Iterator iterator = entrys.iterator(); iterator.hasNext();) {
Entry> entry = (Entry>) iterator.next();
String key = entry.getKey();
Object value = map.get(key);
System.err.println("key:" + key + " value:" + value);
}
}
上面的map可以直接使用jsonUtil工具将map装jsonObject对象
jsonutil工具类地址:JSONUtil工具类