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

结果:

相关推荐
上海合宙LuatOS7 天前
LuatOS核心库API——【json 】json 生成和解析库
java·前端·网络·单片机·嵌入式硬件·物联网·json
敲代码的柯基7 天前
一篇文章理解tsconfig.json和vue.config.js
javascript·vue.js·json
万物得其道者成7 天前
前端大整数精度丢失:一次踩坑后的实战解决方案(`json-bigint`)
前端·json
Ai runner8 天前
Show call stack in perfetto from json input
java·前端·json
ID_180079054738 天前
淘宝商品详情API请求的全场景,带json数据参考
服务器·数据库·json
恒云客8 天前
python uv debug launch.json
数据库·python·json
wanderist.9 天前
从 TCP 到 JSON:一次 FastAPI + LLM 生产环境 “Unexpected end of JSON input” 的底层剖析
tcp/ip·json·fastapi
享誉霸王9 天前
15、告别混乱!Vue3复杂项目的规范搭建与基础库封装实战
前端·javascript·vue.js·前端框架·json·firefox·html5
今心上10 天前
关于json的理解测试!!
开发语言·json
强子感冒了11 天前
JSON和XML学习笔记
xml·学习·json