zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    vue 底层面试题
    js第二阶段的面试题
    vue新一轮的面试题
    vue3面试题
    day_33:后端day04Django框架中的视图和请求、响应
    day_37:后端day08Django框架前后端不分离模式实现项目管理系统(增删差改)
    day_36:后端day07Django框架中的ORM数据库操作二
    【漏洞复现】ThinkAdmin v5和v6 未授权列目录任意文件读取(CVE202025540)
    【超详细】安全测试===sqlmap使用心得(零)
    【最新】绕过Outlook 拦截钓鱼链接方式
  • 原文地址:https://www.cnblogs.com/hlkawa/p/6169775.html
Copyright © 2011-2022 走看看