1. 程式人生 > >SpringBoot 中載入resources中資原始檔

SpringBoot 中載入resources中資原始檔

SpringBoot 中載入resources中資原始檔

1、目錄結構

就是普通的SpringBoot工程

2、json內容

{
  "A": {
    "AA": "AAValue",
    "AB": "ABValue"
  },
  "A": {
    "AA": "AAValue",
    "AB": "ABValue"
  }
}

3、依賴包

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</
artifactId
>
<version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.3</version> </dependency>

4、核心程式碼

public static Map<
String, LinkedHashMap<String, String>> tbInfoMap = new ConcurrentHashMap(); static { ObjectMapper objectMapper = new ObjectMapper(); try { tbInfoMap = objectMapper.readValue(Application.class.getClassLoader().getResourceAsStream("tbInfo.json"), ConcurrentHashMap.
class); } catch (IOException e) { e.printStackTrace(); } }