首页 > 其他分享 >Springboot报class path resource [xxxxx.json] cannot be resolved to URL because it dose not exist问题解决

Springboot报class path resource [xxxxx.json] cannot be resolved to URL because it dose not exist问题解决

时间:2024-05-30 10:15:07浏览次数:10  
标签:resolved 文件 because resource json io import class

当Springboot解析resources文件下的json文件时,在本地环境好用,部署到服务器上找不到文件内容 报错class path resource [xxxxx.json] cannot be resolved to URL because it dose not exist

问题排查

(1)pom.xml文件配置

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.json</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/another-resources</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

(2)解析classpath下的文件时,要以流的方式去读取,否则打包后读取不到(此种方式本地和服务器都支持

import org.springframework.core.io.Resource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Service
public class FileService {

    public String readFileFromResourcesFolder() throws IOException {
        Resource resource = new ClassPathResource("data.txt");
        try (InputStream inputStream = resource.getInputStream()) {
            byte[] bdata = FileCopyUtils.copyToByteArray(inputStream);
            return new String(bdata, StandardCharsets.UTF_8);
        }
    }
}

(3)以下解析文件方式(本地支持,服务器不支持

Resource resource = new ClassPathResource("data.txt");
File f = resource.getFile();
String st = FileUtils.readFileToString(f,Charset.defaultCharset());

 

标签:resolved,文件,because,resource,json,io,import,class
From: https://www.cnblogs.com/mylbs123/p/18221796

相关文章

  • Error creating bean with name ‘dataSource‘ defined in class path resource解决
    报错信息ERROR3592—[restartedMain]o.s.boot.SpringApplication:Applicationrunfailedorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname‘dataSource’definedinclasspathresource[org/springframework/boot/autoconfi......
  • 关于Embedded Resource的理解
    EmbeddedResource.NET中使用外部资源时常用的方式都是使用资源文件,作为程序集的一部分发布。资源文件的读取也比较方便,字符串、图片和任何二进制数据,包括任何类型的文件都可以作为资源的项。使用资源文件时VS也会自动生成相应的方法来获取资源,用xml编辑器打开后缀.resx的文件,......
  • Nginx R31 doc-13-Limiting Access to Proxied HTTP Resources 访问限流
    前言大家好,我是老马。很高兴遇到你。我们为java开发者实现了java版本的nginxhttps://github.com/houbb/nginx4j如果你想知道servlet如何处理的,可以参考我的另一个项目:手写从零实现简易版tomcatminicat手写nginx系列如果你对netty不是很熟悉,可以读一下从......
  • 接口报错.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework
    1、报文:.w.s.m.s.DefaultHandlerExceptionResolver:Resolved[org.springframework.http.converter.HttpMessageNotReadableException:JSONparseerror:Unexpectedcharacter('''(code39)):wasexpectingdouble-quotetostartfieldname;nestedex......
  • Job for nginx.service failed because the control process exited with error code.
    使用systemctlstartnginx启动nginx的时候,报错:Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode.See“systemctlstat。我们可以通过命令查看nginx的配置文件是否修改正确[root@localhost/etc/nginx]$sudonginx-tnginx:[emerg]unexpec......
  • IDEA resource下的文件夹内的配置文件不生效,但resource下的配置生效
    问题:我的resource如图所示,运行时发现只有resource下的application.yml被识别到了,但bak里的三个配置没有生效 解决: 在projectstructure中打开molude,打开resource,选中其下不起作用的文件夹,如图中的bak,将其选为resource资源文件,即点击下图位置 之后再重新运行,可以发现bak......
  • nginx 解决 No connection could be made because the target machine actively refus
    已经搭建好的环境突然全部nginx502,localhost拒绝访问报错信息:connect()failed(10061:Noconnectioncouldbemadebecausethetargetmachineactivelyrefusedit)whileconnectingtoupstream,client: 打开cmd,在cmd中ping127.0.0.1会出现下图情况。  ping......
  • Dalsa SDK弹窗报错解决方案-Resource In Use
    问题描述使用Dalsa的CameraLink相机时,代码调用Snap函数和Grab函数时,弹窗报错,详细报错信息是”Errorin"CorXferSelectEX"Resourceinuse“。见下图:解决办法:在每次调用snap函数或者grab函数前,先调用abort函数,将上一帧残留数据强制结束。按照上述办法测试,弹窗报错频率明显较......
  • vitepress使用createContentLoader时遇到["vitepress" resolved to an ESM file. ESM
    在使用vitepress构建一个所有博客的概览页的时候,使用到了createContentLoader这个api,但是在index.data.ts中定义并导出后,在md文件中调用遇到了下面这个问题:Buildfailedwith1error:node_modules/esbuild/lib/main.js:1374:27:ERROR:[plugin:externalize-deps]"vitepress......
  • .Net6 web API (AOP理解--ResourceFilter)
    前沿Aop(AspectorientProgramming),面向切面编程,作为面向对象编程的一种补充,可以在不破坏之前的封装为基础动态增加一些功能;从而让系统更具备扩展性:增加一个缓存功能增加一个日志功能既希望不要违背开闭原则,也希望能够增加新的工能在之前的业务逻辑之前增加了逻辑,在之前......