I've been trying to render dynamic pages in my spring boot application for a while now, but i can't seem to make progress. I've tried almost everything i've seen on the internet from youtube to AI and this is my last resort.
Whenever i access the endpoint that's supposed to display the dynamic page, it just displays a String instead.
This is what my controller looks like:
package com.demo.student1.api;
import com.demo.student1.model.Student;
import com.demo.student1.service.StudentService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Optional;
@RequestMapping("/student")
@RestController
public class StudentController {
private final StudentService studentService;
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping("/home")
public String home(Model model) {
model.addAttribute("username", "John Doe");
return "home";
}
@PostMapping("/register")
public void registerStudent(@RequestBody Student student) {
studentService.registerStudent(student);
}
@GetMapping("/get_student/{id}")
public Optional<Student> getStudent(@PathVariable Long id) {
return studentService.getStudent(id);
}
@GetMapping("/getStudents")
public ArrayList<Student> getStudents() {
return studentService.getStudents();
}
This is what my dynamic page looks like:
<!DOCTYPE html>
<html xmlns:th=";>
<head>
<meta charset="UTF-8">
<title>Welcome Homepage For Students</title>
</head>
<body>
<h1>Welcome to University, <span th:text="${username}"></span> to University!</h1>
</body>
</html>
These are my gradle dependencies:
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.security:spring-security-web")
implementation("org.springframework.boot:spring-boot-starter-security:3.4.1")
implementation("io.jsonwebtoken:jjwt-api:0.12.6")
implementation("org.springframework:spring-webmvc:6.2.2")
implementation("org.apache.tomcat:tomcat-jasper:10.1.34")
// implementation("org.thymeleaf:thymeleaf:3.1.3.RELEASE")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
compileOnly("org.projectlombok:lombok:1.18.36")
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.6")
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.6")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
This is what my log looks like when i run the application and try to hit the endpoint:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.4.1)
2025-02-08T09:24:16.263+01:00 INFO 23567 --- [student-1] [ main] com.demo.student1.Student1Application : Starting Student1Application using Java 21.0.5 with PID 23567 (/home/isla-jr/Documents/se-workspace/learn-java/roadmap/4-web-frameworks/1-basic/student-1/build/classes/java/main started by isla-jr in /home/isla-jr/Documents/se-workspace/learn-java/roadmap/4-web-frameworks/1-basic/student-1)
2025-02-08T09:24:16.265+01:00 INFO 23567 --- [student-1] [ main] com.demo.student1.Student1Application : No active profile set, falling back to 1 default profile: "default"
2025-02-08T09:24:16.814+01:00 INFO 23567 --- [student-1] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2025-02-08T09:24:16.867+01:00 INFO 23567 --- [student-1] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 44 ms. Found 2 JPA repository interfaces.
2025-02-08T09:24:17.313+01:00 INFO 23567 --- [student-1] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2025-02-08T09:24:17.325+01:00 INFO 23567 --- [student-1] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-02-08T09:24:17.325+01:00 INFO 23567 --- [student-1] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.34]
2025-02-08T09:24:17.457+01:00 INFO 23567 --- [student-1] [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2025-02-08T09:24:17.460+01:00 INFO 23567 --- [student-1] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-02-08T09:24:17.460+01:00 INFO 23567 --- [student-1] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1154 ms
2025-02-08T09:24:17.617+01:00 INFO 23567 --- [student-1] [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2025-02-08T09:24:17.660+01:00 INFO 23567 --- [student-1] [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.6.4.Final
2025-02-08T09:24:17.684+01:00 INFO 23567 --- [student-1] [ main] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled
2025-02-08T09:24:17.923+01:00 INFO 23567 --- [student-1] [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer
2025-02-08T09:24:17.945+01:00 INFO 23567 --- [student-1] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2025-02-08T09:24:18.012+01:00 INFO 23567 --- [student-1] [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@7b3a6e95
2025-02-08T09:24:18.013+01:00 INFO 23567 --- [student-1] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2025-02-08T09:24:18.060+01:00 INFO 23567 --- [student-1] [ main] org.hibernate.orm.connections.pooling : HHH10001005: Database info:
Database JDBC URL [Connecting through datasource 'HikariDataSource (HikariPool-1)']
Database driver: undefined/unknown
Database version: 16.3
Autocommit mode: undefined/unknown
Isolation level: undefined/unknown
Minimum pool size: undefined/unknown
Maximum pool size: undefined/unknown
2025-02-08T09:24:18.834+01:00 INFO 23567 --- [student-1] [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2025-02-08T09:24:18.881+01:00 INFO 23567 --- [student-1] [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2025-02-08T09:24:19.173+01:00 INFO 23567 --- [student-1] [ main] eAuthenticationProviderManagerConfigurer : Global AuthenticationManager configured with AuthenticationProvider bean with name authenticationProvider
2025-02-08T09:24:19.173+01:00 WARN 23567 --- [student-1] [ main] r$InitializeUserDetailsManagerConfigurer : Global AuthenticationManager configured with an AuthenticationProvider bean. UserDetailsService beans will not be used by Spring Security for automatically configuring username/password login. Consider removing the AuthenticationProvider bean. Alternatively, consider using the UserDetailsService in a manually instantiated DaoAuthenticationProvider. If the current configuration is intentional, to turn off this warning, increase the logging level of 'org.springframework.security.config.annotation.authentication.configuration.InitializeUserDetailsBeanManagerConfigurer' to ERROR
2025-02-08T09:24:19.182+01:00 WARN 23567 --- [student-1] [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2025-02-08T09:24:19.636+01:00 INFO 23567 --- [student-1] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2025-02-08T09:24:19.645+01:00 INFO 23567 --- [student-1] [ main] com.demo.student1.Student1Application : Started Student1Application in 3.747 seconds (process running for 4.303)
2025-02-08T09:24:45.131+01:00 INFO 23567 --- [student-1] [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2025-02-08T09:24:45.131+01:00 INFO 23567 --- [student-1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2025-02-08T09:24:45.133+01:00 INFO 23567 --- [student-1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Hibernate: select u1_0.id,u1_0.password,u1_0.username from public.users_v1 u1_0 where u1_0.username=?
2025-02-08T09:24:51.499+01:00 WARN 23567 --- [student-1] [nio-8080-exec-7] o.s.web.servlet.PageNotFound : No mapping for GET /favicon.ico
2025-02-08T09:24:51.502+01:00 WARN 23567 --- [student-1] [nio-8080-exec-7] o.s.web.servlet.PageNotFound : No endpoint GET /favicon.ico.
Finally, this is what i encounter every time i hit the endpoint: browser returning a string instead of a dynamic page
I've been trying to render dynamic pages in my spring boot application for a while now, but i can't seem to make progress. I've tried almost everything i've seen on the internet from youtube to AI and this is my last resort.
Whenever i access the endpoint that's supposed to display the dynamic page, it just displays a String instead.
This is what my controller looks like:
package com.demo.student1.api;
import com.demo.student1.model.Student;
import com.demo.student1.service.StudentService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Optional;
@RequestMapping("/student")
@RestController
public class StudentController {
private final StudentService studentService;
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping("/home")
public String home(Model model) {
model.addAttribute("username", "John Doe");
return "home";
}
@PostMapping("/register")
public void registerStudent(@RequestBody Student student) {
studentService.registerStudent(student);
}
@GetMapping("/get_student/{id}")
public Optional<Student> getStudent(@PathVariable Long id) {
return studentService.getStudent(id);
}
@GetMapping("/getStudents")
public ArrayList<Student> getStudents() {
return studentService.getStudents();
}
This is what my dynamic page looks like:
<!DOCTYPE html>
<html xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Welcome Homepage For Students</title>
</head>
<body>
<h1>Welcome to University, <span th:text="${username}"></span> to University!</h1>
</body>
</html>
These are my gradle dependencies:
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.security:spring-security-web")
implementation("org.springframework.boot:spring-boot-starter-security:3.4.1")
implementation("io.jsonwebtoken:jjwt-api:0.12.6")
implementation("org.springframework:spring-webmvc:6.2.2")
implementation("org.apache.tomcat:tomcat-jasper:10.1.34")
// implementation("org.thymeleaf:thymeleaf:3.1.3.RELEASE")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
compileOnly("org.projectlombok:lombok:1.18.36")
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.6")
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.6")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
This is what my log looks like when i run the application and try to hit the endpoint:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.4.1)
2025-02-08T09:24:16.263+01:00 INFO 23567 --- [student-1] [ main] com.demo.student1.Student1Application : Starting Student1Application using Java 21.0.5 with PID 23567 (/home/isla-jr/Documents/se-workspace/learn-java/roadmap/4-web-frameworks/1-basic/student-1/build/classes/java/main started by isla-jr in /home/isla-jr/Documents/se-workspace/learn-java/roadmap/4-web-frameworks/1-basic/student-1)
2025-02-08T09:24:16.265+01:00 INFO 23567 --- [student-1] [ main] com.demo.student1.Student1Application : No active profile set, falling back to 1 default profile: "default"
2025-02-08T09:24:16.814+01:00 INFO 23567 --- [student-1] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2025-02-08T09:24:16.867+01:00 INFO 23567 --- [student-1] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 44 ms. Found 2 JPA repository interfaces.
2025-02-08T09:24:17.313+01:00 INFO 23567 --- [student-1] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2025-02-08T09:24:17.325+01:00 INFO 23567 --- [student-1] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-02-08T09:24:17.325+01:00 INFO 23567 --- [student-1] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.34]
2025-02-08T09:24:17.457+01:00 INFO 23567 --- [student-1] [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2025-02-08T09:24:17.460+01:00 INFO 23567 --- [student-1] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-02-08T09:24:17.460+01:00 INFO 23567 --- [student-1] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1154 ms
2025-02-08T09:24:17.617+01:00 INFO 23567 --- [student-1] [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2025-02-08T09:24:17.660+01:00 INFO 23567 --- [student-1] [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.6.4.Final
2025-02-08T09:24:17.684+01:00 INFO 23567 --- [student-1] [ main] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled
2025-02-08T09:24:17.923+01:00 INFO 23567 --- [student-1] [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer
2025-02-08T09:24:17.945+01:00 INFO 23567 --- [student-1] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2025-02-08T09:24:18.012+01:00 INFO 23567 --- [student-1] [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@7b3a6e95
2025-02-08T09:24:18.013+01:00 INFO 23567 --- [student-1] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2025-02-08T09:24:18.060+01:00 INFO 23567 --- [student-1] [ main] org.hibernate.orm.connections.pooling : HHH10001005: Database info:
Database JDBC URL [Connecting through datasource 'HikariDataSource (HikariPool-1)']
Database driver: undefined/unknown
Database version: 16.3
Autocommit mode: undefined/unknown
Isolation level: undefined/unknown
Minimum pool size: undefined/unknown
Maximum pool size: undefined/unknown
2025-02-08T09:24:18.834+01:00 INFO 23567 --- [student-1] [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2025-02-08T09:24:18.881+01:00 INFO 23567 --- [student-1] [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2025-02-08T09:24:19.173+01:00 INFO 23567 --- [student-1] [ main] eAuthenticationProviderManagerConfigurer : Global AuthenticationManager configured with AuthenticationProvider bean with name authenticationProvider
2025-02-08T09:24:19.173+01:00 WARN 23567 --- [student-1] [ main] r$InitializeUserDetailsManagerConfigurer : Global AuthenticationManager configured with an AuthenticationProvider bean. UserDetailsService beans will not be used by Spring Security for automatically configuring username/password login. Consider removing the AuthenticationProvider bean. Alternatively, consider using the UserDetailsService in a manually instantiated DaoAuthenticationProvider. If the current configuration is intentional, to turn off this warning, increase the logging level of 'org.springframework.security.config.annotation.authentication.configuration.InitializeUserDetailsBeanManagerConfigurer' to ERROR
2025-02-08T09:24:19.182+01:00 WARN 23567 --- [student-1] [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2025-02-08T09:24:19.636+01:00 INFO 23567 --- [student-1] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2025-02-08T09:24:19.645+01:00 INFO 23567 --- [student-1] [ main] com.demo.student1.Student1Application : Started Student1Application in 3.747 seconds (process running for 4.303)
2025-02-08T09:24:45.131+01:00 INFO 23567 --- [student-1] [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2025-02-08T09:24:45.131+01:00 INFO 23567 --- [student-1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2025-02-08T09:24:45.133+01:00 INFO 23567 --- [student-1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Hibernate: select u1_0.id,u1_0.password,u1_0.username from public.users_v1 u1_0 where u1_0.username=?
2025-02-08T09:24:51.499+01:00 WARN 23567 --- [student-1] [nio-8080-exec-7] o.s.web.servlet.PageNotFound : No mapping for GET /favicon.ico
2025-02-08T09:24:51.502+01:00 WARN 23567 --- [student-1] [nio-8080-exec-7] o.s.web.servlet.PageNotFound : No endpoint GET /favicon.ico.
Finally, this is what i encounter every time i hit the endpoint: browser returning a string instead of a dynamic page
Share Improve this question asked Feb 8 at 10:09 IslaIsla 1 New contributor Isla is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1- Hi and welcome to StackOverflow. When receiving an answer that solves your problem, please accept that answer in order to close your question. BR – Roar S. Commented 2 days ago
1 Answer
Reset to default 1Replace your @RestController
with @Controller
.
@RestController
will return the specified content serialized as JSON output whereas @Controller
will intercept the provided template name (here "home"), resolve the template, render your view through the thymeleaf rendering engine, and return the HTML instead.
Also see here.