小编Gre*_*reg的帖子

Spring Boot - 使用ResourceLoader读取文本文件

我正在尝试使用Spring资源加载器读取文本文件,如下所示:

Resource resource  = resourceLoader.getResource("classpath:\\static\\Sample.txt");
Run Code Online (Sandbox Code Playgroud)

该文件位于我的Spring启动项目中: 在此输入图像描述

它在eclipse中运行应用程序时工作正常,但是当我打包应用程序然后使用java -jar运行它时,我得到文件未找到异常:

java.io.FileNotFoundException: class path resource [static/Sample.txt] cannot be resolved to absolute file path because it does not reside in the
 file system: jar:file:/C:/workspace-test/XXX/target/XXX-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/static/Sample.txt
Run Code Online (Sandbox Code Playgroud)

我解压缩了Sample所在的Jar文件:XXX-0.0.1-SNAPSHOT\BOOT-INF\classes\static\Sample.txt

有谁可以帮助我吗 ?

提前致谢!

java spring spring-boot

11
推荐指数
2
解决办法
6万
查看次数

无法在Junit测试中自动装配JpaRepository - Spring启动应用程序

大家好

我正在尝试在Spring启动应用程序中执行Junit测试,Junit应该测试一些CRUD操作,我正在使用Spring Repositories特别是JpaRepository.

存储库calss:

package au.com.bla.bla.bla.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import au.com.bla.bla.bla.entity.Todo;

public interface TodoRepository extends JpaRepository<Todo, Integer> {

}
Run Code Online (Sandbox Code Playgroud)

TodoController类

package au.com.bla.bla.bla.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import au.com.bla.bla.bla.entity.Todo;
import au.com.bla.bla.bla.repository.TodoRepository;


import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
public class TodoController
{
    static final String TEXT = "text";

    @Autowired
    private TodoRepository todoRepository;

...
    @RequestMapping(path = "/todo", method = RequestMethod.POST)
    public Todo create(@RequestBody Map<String, Object> body)
    {
        Todo todo = new Todo();
        todo.setCompleted(Boolean.FALSE);
        todo.setText(body.get(TEXT).toString()); …
Run Code Online (Sandbox Code Playgroud)

java junit spring repository autowired

5
推荐指数
1
解决办法
7822
查看次数

标签 统计

java ×2

spring ×2

autowired ×1

junit ×1

repository ×1

spring-boot ×1