在开发Elasticsearch的应用过程中,需要读取索引相关的JSON Settings、MAppings设置,将JSON文档存放到项目的resources 目录,然后读取并调用Elasticsearch API 执行。
POM依赖
com.alibaba
fastjson
1.2.56
读取JSON工具类
package com.fanxb.esdemo.util;
import JAVA.io.*;
public class JsonUtil {
/**
* 读取json文件,返回json串
* @param fileName
* @return
*/
public static String readJsonFile(String fileName) {
String jsonStr = "";
try {
File jsonFile = new File(fileName);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();