Issue
In my SpringBoot application I have 2 controllers. I am trying to render JSP page but unable to do so. I have <app-root>
in JSP page which points to Angular application.
On hitting http://localhost:8081//api/project1/mission/home
I get below error.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jan 14 00:15:58 CST 2022
There was an unexpected error (type=Not Found, status=404).
I have tried many solutions but none of them are working. I need help. Thank you
application.yaml
server:
port: 8081
servlet:
context-path: /api/project1/mission
tomcat:
max-http-header-size: 72636B
management:
endpoints:
web:
base-path:
exposure:
include: health
path-mapping:
health: healthcheck
health:
solr:
enabled: false
redis:
enabled: false
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
welcome.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<base href="/api/project1/mission/"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/dist/polyfills.js" defer></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/dist/runtime.js" defer></script>
</head>
<body>
<div id="mission-app-ctnr">
<app-root></app-root>
</div>
</body>
</html>
FirstController
package com.google.mission.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
@RequestMapping(path = "/")
public class FirstController {
private String message = "Hi";
@GetMapping
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
System.out.println(this.message);
return "welcome";
}
}
AnotherController.js
package com.google.mission.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
@RequestMapping(path = "/home")
public class AnotherController {
private String message = "Hi";
@GetMapping
public String serveTemplate(Map<String, Object> model) {
model.put("message", this.message);
return "welcome";
}
}
Solution
Adding the below dependency should solve the error.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Answered By - Ashish Shah
Answer Checked By - Marie Seifert (JavaFixing Admin)