Lorsque je l'exécute avec un pot, je ne trouve pas la ressource lorsque deux barres obliques comme celle ci-dessous se chevauchent.
            InputStream resource1 = this.getClass().getClassLoader().getResourceAsStream("test//hello.txt");
package work.inabajun;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) throws IOException {
        new Test().getHelloText();
    }
    private static class Test {
        public void getHelloText() throws IOException {
            //Une barre oblique
            InputStream resource1 = this.getClass().getClassLoader().getResourceAsStream("test/hello.txt");
            if( resource1 != null) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource1));
                System.out.println("Resource1:" + bufferedReader.readLine());
            } else {
                System.out.println("Resource1 is null.");
            }
            //Deux barres obliques
            InputStream resource2 = this.getClass().getClassLoader().getResourceAsStream("test//hello.txt");
            if( resource2 != null) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource2));
                System.out.println("Resource2:" + bufferedReader.readLine());
            } else {
                System.out.println("Resource2 is null.");
            }
        }
    }
}
$ java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=60858:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8  -classpath /Users/inabajunmr/test/untitled/out/production/untitled work.inabajun.Main
objc[7462]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x1023c94c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x103bf04e0). One of the two will be used. Which one is undefined.
Resource1:abcde12345
Resource2:abcde12345
Ressource1 et Ressource2 peuvent être acquises.
$ java -jar untitled_jar/untitled.jar 
Resource1:abcde12345
Resource2 is null.
Resource2 devient nul.
J'ai écrit le code suivant avec Spring Boot + Thymeleaf et cela fonctionnait normalement lors du démarrage avec Gradle ou IDE
	@GetMapping("/list.html")
	public String viewInput(final Model model) {
		return "/resource/list";
	}
Lorsque je l'ai déployé dans l'environnement, j'étais en colère parce qu'il n'y avait pas de modèle.
2018-03-21 11:58:56.840 ERROR 26381 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/resource/list", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/resource/list", template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:870)
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:354)
La différence dans ce comportement. https://github.com/spring-projects/spring-boot/issues/1744
Après avoir supprimé la barre oblique principale, cela fonctionnait avec l'une ou l'autre des méthodes de démarrage.
	@GetMapping("/list.html")
	public String viewInput(final Model model) {
		return "resource/list";
	}
        Recommended Posts