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

结果:

相关推荐
测试杂货铺7 小时前
Jmeter(六):json断言元件,jmeter参数化实现
jmeter·json
专注VB编程开发20年12 小时前
C#,VB.NET从JSON数据里提取数组中的对象节点值
c#·json·.net
草履虫建模17 小时前
Postman - API 调试与开发工具 - 标准使用流程
java·测试工具·spring·json·测试用例·postman·集成学习
奔跑的蜗牛AZ10 天前
TiDB 字符串行转列与 JSON 数据查询优化知识笔记
笔记·json·tidb
jarctique12 天前
java 找出两个json文件的不同之处
java·json
wtsolutions13 天前
Excel to JSON online converter, flat and nested JSON converter
json·excel·excel-to-json·wtsolutions
漫谈网络14 天前
JSON 数据格式详解
网络·python·json·数据格式
lwb_011814 天前
Spring MVC参数绑定终极手册:单&多参对象集合JSON文件上传精讲
spring·json·mvc
电商数据girl15 天前
【经验分享】浅谈京东商品SKU接口的技术实现原理
java·开发语言·前端·数据库·经验分享·eclipse·json
亚林瓜子15 天前
AWS S3拒绝非https的请求访问
https·云计算·json·ssl·aws·s3