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

结果:

相关推荐
Json_181790144808 小时前
商品详情接口使用方法和对接流程如下
大数据·json
ZhongruiRao1 天前
Springboot+PostgreSQL+MybatisPlus存储JSON或List、数组(Array)数据
spring boot·postgresql·json
华农第一蒟蒻1 天前
Java中JWT(JSON Web Token)的运用
java·前端·spring boot·json·token
胡耀超1 天前
知识图谱入门——8: KG开发常见数据格式:OWL、RDF、XML、GraphML、JSON、CSV。
xml·json·知识图谱·csv·owl·graphml·gml
x-cmd2 天前
[241005] 14 款最佳免费开源图像处理库 | PostgreSQL 17 正式发布
数据库·图像处理·sql·安全·postgresql·开源·json
先知demons2 天前
js将对象的键和值分别归纳进对象,并将多层对象转化成数据的方法
javascript·vue.js·json
Midsummer啦啦啦2 天前
Python字符串转JSON格式指南
开发语言·python·json
前端 贾公子2 天前
Express内置的中间件(express.json和express.urlencoded)格式的请求体数据
中间件·json·express
迷失蒲公英3 天前
在线JSON可视化工具--支持缩放
json·在线json可视化·在线json格式化
bug菌¹5 天前
滚雪球学MySQL[8.3讲]:数据库中的JSON与全文检索详解:从数据存储到全文索引的高效使用
数据库·mysql·json·全文索引