>  기사  >  Java  >  SpringBoot는 리소스 디렉터리의 JSON 파일을 어떻게 읽나요?

SpringBoot는 리소스 디렉터리의 JSON 파일을 어떻게 읽나요?

王林
王林앞으로
2023-05-16 13:25:162953검색

Idea

Spring의 ResourceUtils를 사용하여 리소스 디렉터리에 있는 json 파일을 읽습니다.

common-io를 사용하여 읽은 파일을 json 문자열로 변환하세요.

fastjson을 사용하여 json 문자열을 객체로 역직렬화합니다.

Example

SpringBoot는 리소스 디렉터리의 JSON 파일을 어떻게 읽나요?

1. Maven dependency

pom.xml, 주로 common-io와 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 리소스 파일

notice.json, 사용할 json 콘텐츠를 간단히 나열하세요.

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

3. json 서비스 읽기

3.1. 인터페이스 정의

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. 인터페이스 구현

자세한 내용은 코드에서 구현을 볼 수 있습니다. ResourceUtils를 통해 통지.json을 읽습니다. 이 json 파일은 common-io의 FileUtils를 통해 json 문자열로 변환되고, json 객체는 fastjson의 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. 테스트 인터페이스

간단한 통합 테스트를 작성하고 위에서 작성한 서비스를 주입한 후 메소드를 실행하고 실행 결과를 인쇄합니다.

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());
    }
}

SpringBoot는 리소스 디렉터리의 JSON 파일을 어떻게 읽나요?

json 파일의 내용이 정상적으로 출력되는 것을 볼 수 있어 우리 프로그램이 정확하다는 것을 알 수 있습니다.

위 내용은 SpringBoot는 리소스 디렉터리의 JSON 파일을 어떻게 읽나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제