SpringBoot读取json文件

使用SpringBoot读取json文件作为接口,前端Vue可以通过跨域访问接口数据

一、创建SpringBoot 文件

创建一个 SpringBoot 文件,文件结构目录如下:

二、在pom.xml添加依赖

java 复制代码
  <!--Spring Boot 依赖-->  
    <parent>
        <artifactId>spring-boot-test</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.1.3.RELEASE</version>
    </parent>

<dependencies>
<!--        Spring Boot依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--视图依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--实体类代码简化依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
        <!--        json处理依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>

        <!--        io依赖-->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>

三、在 com 目录下创建 App.java

App.java作为启动类

代码如下:

java 复制代码
​package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}

四、创建IndexController.java

在com目录下创建controller包,创建IndexController.java控制器类,用于处理特定的HTTP GET请求

java 复制代码
package com.controller;


import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class IndexController {
    // 资源加载器,用于加载外部资源
    private final ResourceLoader resourceLoader;
    // 构造函数注入资源加载器
    public IndexController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    // 定义GET请求的端点"/json",并指定响应类型为JSON
    @GetMapping(value = "/json", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getJsonData() throws IOException {
        // 加载资源,假设JSON文件名为moviedata.json,位于classpath下
        Resource resource = resourceLoader.getResource("classpath:moviedata.json");
        // 读取文件内容并转换为字符串
        String jsonContent = org.apache.commons.io.FileUtils.readFileToString(resource.getFile(), "UTF-8");
        return jsonContent;
    }
}

五、启动

在App.java启动程序

访问端口号

java 复制代码
http://localhost:8080/json

结果:

相关推荐
远方16092 天前
16-Oracle 23 ai-JSON-Relational Duality-知识准备
数据库·oracle·json
sc写算法2 天前
基于nlohmann/json 实现 从C++对象转换成JSON数据格式
开发语言·c++·json
酷爱码3 天前
Spring Boot项目中JSON解析库的深度解析与应用实践
spring boot·后端·json
不惑_3 天前
用 PyQt5 打造一个可视化 JSON 数据解析工具
开发语言·qt·json
漫游者Nova3 天前
PDF转Markdown/JSON软件MinerU最新1.3.12版整合包下载
pdf·json·markdown·mineru
snow@li4 天前
vue3+ts+vite:详细、完整的 tsconfig.json 示例 / 常见配置项及其用途
json·tsconfig.json
南郁4 天前
007-nlohmann/json 项目应用-C++开源库108杰
c++·开源·json·nlohmann·现代c++·d2school·108杰
紫乾20144 天前
idea json生成实体类
java·json·intellij-idea
愿你天黑有灯下雨有伞5 天前
MyBatis-Plus LambdaQuery 高级用法:JSON 路径查询与条件拼接的全场景解析
mysql·json·mybatis
wtsolutions5 天前
JSON to Excel 3.0.0 版本发布 - 从Excel插件到Web应用的转变
json·excel·json-to-excel·wtsolutions