Home  >  Article  >  Java  >  How does SpringBoot read JSON files in the resource directory?

How does SpringBoot read JSON files in the resource directory?

王林
王林forward
2023-05-16 13:25:163042browse

Idea

Use Spring's ResourceUtils to read the json file in the resource directory.

Use common-io to convert the read file into a json string.

Use fastjson to deserialize json strings into objects.

Example

How does SpringBoot read JSON files in the resource directory?

1.Maven depends on

pom.xml, mainly the introduction of common-io and fastjson.

<!-- 资源目录资源文件读取 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

        <!-- 反序列化json字符串 -->
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.14</version>
        </dependency>

2.json resource file

notice.json, simply list the json content to be used.

[
  {
    "title": "新功能xxx上线",
    "content": "支持xxx"
  },
  {
    "title": "旧功能xxx下线",
    "content": "不支持xxx"
  }
]

3. Read json Service

3.1. Define the interface

package com.example.springbootjson.service;

import com.example.springbootjson.domain.NoticeInfo;

import java.io.IOException;
import java.util.List;

/**
 * @author hongcunlin
 */
public interface NoticeService {
    /**
     * 获取公告
     *
     * @return 公告
     * @throws IOException 文件
     */
    List<NoticeInfo> getNoticeInfoList() throws IOException;
}

3.2. Implement the interface

This can be said to be the core part of this article , you can see the implementation in the code for details, read the notice.json json file through ResourceUtils, convert the file into a json string through common-io's FileUtils, and deserialize the json object through fastjson's JSON.

package com.example.springbootjson.service.impl;

import com.alibaba.fastjson2.JSON;
import com.example.springbootjson.domain.NoticeInfo;
import com.example.springbootjson.service.NoticeService;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author hongcunlin
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Override
    public List<NoticeInfo> getNoticeInfoList() throws IOException {
        File file = ResourceUtils.getFile("classpath:notice.json");
        String json = FileUtils.readFileToString(file, "UTF-8");
        List<NoticeInfo> noticeInfoList = JSON.parseArray(json, NoticeInfo.class);
        return noticeInfoList;
    }
}

4. Test interface

Write a simple integration test, inject the Service written above, execute the method, and print the execution results.

package com.example.springbootjson;

import com.example.springbootjson.service.NoticeService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;

@SpringBootTest
class SpringbootJsonApplicationTests {
    @Resource
    private NoticeService noticeService;

    @Test
    void contextLoads() throws IOException {
        System.out.println(noticeService.getNoticeInfoList());
    }
}

How does SpringBoot read JSON files in the resource directory?

You can see that the content in the json file can be output normally, indicating that our program is correct.

The above is the detailed content of How does SpringBoot read JSON files in the resource directory?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete