I’m new to spring boot and built the MVC project but I’m facing the error:
Error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Dec 07 18:22:16 IST 2024
There was an unexpected error (type=Not Found, status=404).
Below is the complete code.
DemoGoBootSpringMvcApplication.java
package com.example.prs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(value= {"com.infosys.prs.controller"})
@ComponentScan(value= {"com.infosys.prs.service"})
public class DemoGoBootSpringMvcApplication {
public static void main(String[] args) {
SpringApplication.run(DemoGoBootSpringMvcApplication.class, args);
}
}
HomeController.java
package com.example.prs.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@GetMapping(value="/")
public ModelAndView getHomeDetails() {
return new ModelAndView("infyGoHome",""," ");
}
}
RegistrationController.java
package com.example.prs.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.prs.exception.UserIdAlreadyPresentException;
import com.example.prs.model.User;
import com.example.prs.service.RegistrationService;
import org.springframework.ui.Model;
import jakarta.validation.Valid;
@Controller
public class RegistrationController {
@Autowired
@Qualifier("registrationService")
private RegistrationService registrationService;
@Autowired
private Environment environment;
private String command = "command";
private String register = "register";
@GetMapping(value="/register")
public ModelAndView register(Model model) {
return new ModelAndView(register,command,new User());
}
@PostMapping(value="/registerUser")
public ModelAndView addCustomer(@Valid @ModelAttribute("command") User user, BindingResult result, Model model) {
ModelAndView modelAndView = new ModelAndView();
if(result.hasErrors()) {
modelAndView = new ModelAndView(register,command,user);
}else {
try {
registrationService.registerUser(user);
modelAndView = new ModelAndView(register,command,user);
modelAndView.addObject("succeedMessage",environment.getProperty("RegistrationController.SUCCESSFUL_REGISTRATION"));
}catch(UserIdAlreadyPresentException e) {
if(e.getMessage().contains("RegistrationService")) {
modelAndView = new ModelAndView(register);
modelAndView.addObject(command,user);
modelAndView.addObject("message",environment.getProperty(e.getMessage()));
}
}
}
return modelAndView;
}
}
UserEntity.java
package com.example.prs.entity;
import jakarta.persistence.*;
@Entity
@Access(AccessType.FIELD)
@Table(name="USER_DETAILS")
public class UserEntity {
@Id
@Column(name="userid")
private String userId;
private String password;
private String name;
private String city;
private String email;
private String phone;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
User.java
package com.example.prs.model;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
public class User {
@NotNull(message="USerID must not be blank.")
@Size(min=4, max=15, message="USerId must be between 4 to 15 characters")
private String userId;
@NotNull(message="Password must not be blank.")
@Size(min=8, max=15, message="USerId must be between 8 to 15 characters")
private String password;
@NotNull(message="Name must not be blank.")
@Size(min=4, max=15, message="Name must be between 4 to 15 characters")
private String name;
@NotNull(message="City must not be blank.")
private String city;
@NotNull(message="Email must not be blank.")
@Email
private String email;
@NotNull(message="PhoneNumber must not be blank.")
@Size(min=10, max=10, message="PhoneNumber must be etween 10 digits.")
private String phone;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
UserRepository.java
package com.example.prs.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.prs.entity.UserEntity;
@Repository
public interface UserRepository extends JpaRepository<UserEntity, String>{
}
RegistrationService.java
package com.example.prs.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.example.prs.entity.UserEntity;
import com.example.prs.exception.UserIdAlreadyPresentException;
import com.example.prs.model.User;
import com.example.prs.repository.UserRepository;
@Service
@Component
public class RegistrationService {
@Autowired
public UserRepository userRepository;
public void registerUser(User user) throws UserIdAlreadyPresentException{
boolean ue = userRepository.existsById(user.getUserId());
if(ue) throw new UserIdAlreadyPresentException("RegistrationService.USERID_PRESENT");
UserEntity userEntity = new UserEntity();
userEntity.setCity(user.getCity());
userEntity.setEmail(user.getEmail());
userEntity.setPassword(user.getPassword());
userEntity.setPhone(user.getPhone());
userEntity.setUserId(user.getUserId());
userRepository.saveAndFlush(userEntity);
}
}
application.properties
spring.application.name=DemoGoBoot_SpringMVC
server.port=8080
server.servlet.context-path = /register
spring.mvc.view.prefix = WEB-INF/pages/
spring.mvc.view.suffix = .jsp
spring.datasource.url = jdbc:mysql://localhost/db_practice_dev
spring.datasource.username = root
spring.datasource.password = root
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.prs</groupId>
<artifactId>DemoGoBoot_SpringMVC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>DemoGoBoot_SpringMVC</name>
<description>Implementing web layer with Spring MVC</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed.japser</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Structure of my project
Structure of my project
Please help me with the error because I’m stuck with further learnings here
1