When I run it in a jar, I can't find the resource when two slashes like the one below overlap.
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 {
//One slash
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.");
}
//Two slashes
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
Both Resource1 and Resource2 can be acquired.
$ java -jar untitled_jar/untitled.jar
Resource1:abcde12345
Resource2 is null.
Resource2 becomes null.
When I wrote the following code with Spring Boot + Thymeleaf, it worked normally when booting with Gradle or IDE
@GetMapping("/list.html")
public String viewInput(final Model model) {
return "/resource/list";
}
When I deployed it to the environment, I was angry that there was no template.
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)
The difference in this behavior. https://github.com/spring-projects/spring-boot/issues/1744
After removing the leading slash, it worked with either startup method.
@GetMapping("/list.html")
public String viewInput(final Model model) {
return "resource/list";
}
Recommended Posts