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

结果:

相关推荐
誰能久伴不乏11 小时前
Qt C++ 解析 JSON 完全指南:从核心概念到工业级实战
c++·qt·json
空空潍13 小时前
使用Coze工作流API实现结构化输出
json·工作流·coze
神奇的代码在哪里13 小时前
【单机离线版】excel转json软件,纯HTML+JS零依赖实现Excel转JSON工具,一个index.html搞定所有转换!
html·json·excel·excel转json·xlsx转json·xls转json
爱滑雪的码农1 天前
Java基础二十:JSON 数据解析、对象与 JSON 互转逻辑
json
吴声子夜歌2 天前
PlantUML——显示JSON数据
json
祀爱2 天前
ControllerBase 类将对象转换为 JSON 格式并返回前端的方法
前端·json·asp.net
weelinking2 天前
【产品】11_实现后端接口——数据在背后如何流动
java·人工智能·python·sql·oracle·json·ai编程
油炸自行车3 天前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
NiceCloud喜云3 天前
Claude Code 跑 HyperFrames 实测:本地生成 AI 视频素材全流程
java·运维·人工智能·自动化·json·音视频·飞书
逍遥德3 天前
PostgreSQL --- JSON 函数详解
数据库·sql·postgresql·json