See http://stackoverflow.com/questions/26493831/spring-boot-thymeleaf-not-resolving-fragments-after-packaging. I'm not sure that there's anything we can do but I'd like to investigate.
It seems to be a basic limitation of the JDK. This small class:
public class TestResourceLoading {
public static void main(String[] args) throws IOException {
System.out.println(Application.class.getClassLoader().getResourceAsStream("foo/bar"));
System.out.println(Application.class.getClassLoader().getResourceAsStream("foo//bar"));
}
}
Run in an IDE:
java.io.BufferedInputStream@74e22632
java.io.BufferedInputStream@2b39d891
Run with java -jar:
sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@61bbe9ba
null
We could possibly do something in LanchedURLClassLoader to turn more than one consecutive slash into a single slash but I'm not sure it's worth it.
Seems like this might be a common mistake, it's been raised again in #2057. Striping double slashes might be an option but we need to check that this won't break existing code.
Following on from #2507...with more (poss. important?) details.
I originally didn't have preceding '/' characters on my view names. The problem came when I introduced spring security:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/home")
.failureUrl("/login?error")
.permitAll()
.and()
.logout()
.invalidateHttpSession(true)
.logoutSuccessUrl("/login?logout")
.permitAll();
}
I found that I could not use simply 'login' instead of '/login', etc. (maybe SS is dong something different to Thymeleaf?) So I needed to find a solution that preserved the slash at the beginning of view names, while stripping the duplicate away. Thus I adopted the classpath configuration:
spring:
thymeleaf:
cache: false
prefix: classpath:/templates
Having review the behavior of the standard URLClassLoader I don't think double slash detection is something that we should do.
Given the following code:
InputStream url = Thing.class.getClassLoader().getResourceAsStream("sample//test.txt");
System.out.println(url != null);
Running the application from an IDE will print return true. Package into a jar and running using java -cp ./target/test-0.0.1-SNAPSHOT.jar sample.Test will return false.
This shows that our fat jar support is working in the same way as the standard JDK and I think it's best to keep consistency rather than introduce a difference and potentially break things in other ways.
If it's a common mistake, I think giving a warning message would be helpful.
Just as an extra information. I have a similar issue and there it works in the IDE and Tomcat. So I think the default jdk is inconsistent, would be great to get a warning.
Yo tenia el mismo problema, la soluci贸n la encontr茅 en este link. https://stackoverflow.com/questions/27249078/spring-boot-gives-templateinputexception-error-resolving-template-when-runnin
Okay, I found where you have to take a close look. In your Controller classes, when you use @RequestMapping on class, no leading slash (e.g. @RequestMapping("property")). On Method you need a leading slash (e.g. @GetMapping("/list")) and the return value no leading slash (e.g. return "property/list";)
An Example Snippet
@Controller
@RequestMapping("property")
public class PropertyController {
@NonNull PropertyService service;
@GetMapping("/list")
public String list() {
return "property/list";
}
}
Is this issue resolved?
I've tried with version 1.5.10 and seems to be still happening.
I have the following code:
@Controller
public TestController {
@RequestMapping(path= "/client/add", method = RequestMethod.GET)
public String add() {
return "/client/add"; // STARTS WITH A SLASH
}
}
It works starting with Spring Boot using Maven: ./mvnw spring-boot:run.
It does not work with the jar: java -jar myapp.jar.
If I change the code above to return the template without the slash in the beginning, it works on both:
@Controller
public TestController {
@RequestMapping(path= "/client/add", method = RequestMethod.GET)
public String add() {
return "client/add"; // DOES NOT START WITH A SLASH
}
}
Is it the expected behaviour or this issue should've fixed that?
When you return the URI starting with a slash, you always send a double slash. The difference is that starting with Spring the file system deals with this issue, which doesn't do the JVM, when you run the .jar.
I'm using Spring Boot 2.0.0.RELEASE and I'm experiencing this, I've tried removing slashes on my RequestMapping but I'm still getting HTTP 500. Is there a solution for this?
Have you checked the links in the .html (Thymeleaf template)? There you could be generating double slashes as well.
@oratengG4S we don't use the tracker for questions. Please ask on StackOverflow or join us on Gitter.