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";
}
}
3
Answers
Your
context-path
is incorrect. You can set it to either/
(for the root webapp, which is the default value) or a directory (e.g./myapp
), but not a subdirectory like you did (/api/project1/mission
).So I suggest that you remove the
context-path
setting from your application.yaml and map your controller to/api/project1/mission/home
:Putting item 2. in short: "It works only with
.war
archives"!To change your jar to a war, follow these (few, simple) steps:
ServletInitializer
to your application. (also best sample from: https://start.spring.io/#!packaging=war )Adding the below dependency should solve the error.