yaml文件的读取以及其工具类

由于在工作中会出现一系列以键值对形式选项参数对应,可以通过yaml文件进行读取文件流。

type.yml

#计划审核表-查询类型
TCX_JHSBB_AUDIT_searchSblx:
  - valueData: 1
    displayName: 待审核
  - valueData: 2
    displayName: 已审核

YamlUtil

package com.yufei.core.util.conf;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;

import com.yufei.core.entity.SelectModel;
import com.yufei.core.util.PathUtil;

/**
 * type.yml 配置文件对应下拉集合显示
 * @author Lenovo
 */
public class YamlUtil {
	
	
	// 计划审核表-查询类型(计划审核、配合审核)
	public static final String TCX_JHSBB_AUDIT_SEARCHSBLX = "TCX_JHSBB_AUDIT_searchSblx"; // 查询类型
	
	
	/**
	 * 读取下拉状态配置参数List
	 * @param typeName
	 * @return
	 * @throws IOException
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public static List<SelectModel> getTypePropertie(String typeName) throws IOException{
		String yamlPath = PathUtil.getClassResources() + "conf/type.yml"; // yaml路径
		File yamlFile = new File(yamlPath);
		Yaml yaml = new Yaml();
		HashMap map = yaml.loadAs(new FileInputStream(yamlFile), HashMap.class);
		
		List<SelectModel> list = new ArrayList<SelectModel>();
		List contentList = (List)map.get(typeName);
		for (Object item : contentList){
			list.add(new SelectModel(((HashMap)item).get("valueData").toString(), ((HashMap)item).get("displayName").toString()));
		}
		return list;
	}
	
	/**
	 * 读取下拉状态配置参数返回map
	 * @param typeName
	 * @return
	 * @throws IOException
	 */
	public static Map<String,Object> getTypePropertieMap(String typeName) throws IOException{
		String yamlPath = PathUtil.getClassResources() + "conf/type.yml"; // yaml路径
		File yamlFile = new File(yamlPath);
		Yaml yaml = new Yaml();
		HashMap hashMap = yaml.loadAs(new FileInputStream(yamlFile), HashMap.class);
		
		Map<String,Object> map = new HashMap<String,Object>();
		List contentList = (List)hashMap.get(typeName);
		for (Object item : contentList){
			map.put(((HashMap)item).get("valueData").toString(), ((HashMap)item).get("displayName").toString());
		}
		return map;
	}
}

注意:这里需要导入一个包,可以在Maven中引入

	<dependency>
		    <groupId>org.yaml</groupId>
		    <artifactId>snakeyaml</artifactId>
		    <version>1.17</version>
		</dependency>

上面中还引入了一个PathUtil的路径工具类

package com.yufei.core.util;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 路径工具类
 * 
 * @author
 * 
 */
public class PathUtil {

	/**
	 * 图片访问路径
	 * 
	 * @param pathType
	 *            图片类型 visit-访问;save-保存
	 * @param pathCategory
	 *            图片类别,如:话题图片-topic、话题回复图片-reply、商家图片
	 * @return
	 */
	public static String getPicturePath(String pathType, String pathCategory) {
		String strResult = "";
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		StringBuffer strBuf = new StringBuffer();
		if ("visit".equals(pathType)) {
		} else if ("save".equals(pathType)) {
			String projectPath = PublicUtil.getPorjectPath().replaceAll("\\\\",
					"/");
			projectPath = splitString(projectPath, "bin/");

			strBuf.append(projectPath);
			strBuf.append("webapps/ROOT/");
		}

		strResult = strBuf.toString();

		return strResult;
	}

	private static String splitString(String str, String param) {
		String result = str;

		if (str.contains(param)) {
			int start = str.indexOf(param);
			result = str.substring(0, start);
		}

		return result;
	}
	
	/*
	 * 获取classpath1
	 */
	public static String getClasspath(){
		String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../").replaceAll("file:/", "").replaceAll("%20", " ").trim();	
		if(path.indexOf(":") != 1){
			path = File.separator + path;
		}
		return path;
	}
	
	/*
	 * 获取classpath2
	 */
	public static String getClassResources(){
		String path =  (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))).replaceAll("file:/", "").replaceAll("%20", " ").trim();	
		if(path.indexOf(":") != 1){
			path = File.separator + path;
		}
		return path;
	}
	
	public static String PathAddress() {
		String strResult = "";

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();

		StringBuffer strBuf = new StringBuffer();

		strBuf.append(request.getScheme() + "://");
		strBuf.append(request.getServerName() + ":");
		strBuf.append(request.getServerPort() + "");

		strBuf.append(request.getContextPath() + "/");

		strResult = strBuf.toString();// +"ss/";//加入项目的名称

		return strResult;
	}
	
	
}

SelectModel

package com.yufei.core.entity;


/**
 * 下拉绑定对象
 * @author zhanghd 2017-07-27
 *
 */
public class SelectModel {
	public SelectModel() {
		super();
	}
	
	public SelectModel(String valueData, String displayName) {
		super();
		this.valueData = valueData;
		this.displayName = displayName;
	}

	/**
	 * 下拉值
	 */
	private String valueData;
	/**
	 * 下拉显示内容
	 */
	private String displayName;

	public String getValueData() {
		return valueData;
	}

	public void setValueData(String valueData) {
		this.valueData = valueData;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
}

注意:在测试过程中,报了一个Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1

原因,type.yml中的指定配置文件编码不是UTF-8的,转换成UTF-8就行了。

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值