spring注解读取json文件

开发时候在接口没有提供的时候,可以用json文件提前模拟接口数据或者自己开发些工具类的网站不想带数据库也可以用本地json数据

实现原理是利用自定义参数注解@Value获取到本地json文件,然后利用Scanner来读取json文件

 

1.service层

package com.syp.spring.service;

import java.io.File;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;


@Service
public class TestService {
    @Value(value="classpath:resource/rest.json")
    private  Resource data;    
    
    public String getData(){
        try {
            File file = data.getFile();
            long t0 = System.nanoTime();
            String jsonData = this.jsonRead(file);
            long t1 = System.nanoTime();
            long millis = TimeUnit.NANOSECONDS.toMillis(t1-t0);
            System.out.println(millis +"ms");
            return jsonData;
        } catch (Exception e) {
            return null;
        }
    }
    /**
     *     读取文件类容为字符串
     * @param file
     * @return
     */
      private String jsonRead(File file){
            Scanner scanner = null;
            StringBuilder buffer = new StringBuilder();
            try {
                scanner = new Scanner(file, "utf-8");
                while (scanner.hasNextLine()) {
                    buffer.append(scanner.nextLine());
                }
            } catch (Exception e) {
                
            } finally {
                if (scanner != null) {
                    scanner.close();
                }
            }
            return buffer.toString();
        }

}

2.controller层

package com.syp.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.syp.spring.service.TestService;

@Controller
@RequestMapping(value="/syp/spring")
public class TestController {
    
    @Autowired 
    private TestService testService;
    
    @RequestMapping(method=RequestMethod.GET)
    @ResponseBody
    public String test(){
        return testService.getData();
    }
}

 

转载于:https://www.cnblogs.com/hlkawa/p/6169775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,读取JSON文件并分页通常需要使用`java.io`、`java.nio.file`或者第三方库如Jackson、Gson或者Spring的`@RestController`和`@GetMapping`注解等。以下是一个简单的示例,假设你已经有了一个JSON文件,它存储了一组数据: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; @RestController public class JsonPaginationController { private static final ObjectMapper objectMapper = new ObjectMapper(); @GetMapping("/api/data") public List<DataObject> getData(@RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) { try { File jsonFile = new File("path/to/your/json/file.json"); List<DataObject> allData = objectMapper.readValue(jsonFile, new TypeReference<List<DataObject>>() {}); // 分页处理 int startIndex = page * pageSize; List<DataObject> paginatedData = allData.subList(startIndex, startIndex + pageSize); return paginatedData; } catch (IOException e) { throw new RuntimeException("Failed to read JSON file", e); } } // 假设 DataObject 是你的JSON对象对应的实体类 private static class DataObject {} } ``` 在这个例子中,`getData`方法接收两个参数:`page`表示当前请求的页码,`pageSize`表示每页显示的数据量。通过Jackson将JSON文件转换为Java对象列表,然后进行分页操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值