Проект Spring boot MVC не работает из-за статуса = 404 Spring bootJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Проект Spring boot MVC не работает из-за статуса = 404 Spring boot

Сообщение Anonymous »

Я новичок в Spring Boot и создал проект MVC, но столкнулся с ошибкой:
Ошибка:
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).

Ниже приведен полный код.
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;
}

}

Пользователь.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{

}

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

org.springframework.boot
spring-boot-starter-parent
3.4.0


com.example.prs
DemoGoBoot_SpringMVC
0.0.1-SNAPSHOT
war
DemoGoBoot_SpringMVC
Implementing web layer with Spring MVC














17



org.springframework.boot
spring-boot-starter-data-jpa


org.springframework.boot
spring-boot-starter-web



com.mysql
mysql-connector-j
runtime


org.springframework.boot
spring-boot-starter-tomcat
provided


org.springframework.boot
spring-boot-starter-test
test


org.springframework.boot
spring-boot-starter-validation


org.apache.tomcat.embed
tomcat-embed.japser
provided


jstl
jstl
1.2
provided






org.springframework.boot
spring-boot-maven-plugin






Структура моего проекта
Структура моего проекта
[img]https://i.sstatic .net/XIkBf4Ec.png[/img]

Пожалуйста, помогите мне с ошибкой, потому что я застрял здесь с дальнейшим изучением.
Ошибка, которую я столкнулся после включения отладки журналы:
2024-12-07T18:22:16.513+05:30 DEBUG 5800

[DemoGoBoot_SpringMVC] [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet

: GET "/register/",

parameters-{}

2024-12-07T18:22:16.536+05:30 DEBUG 5800

[DemoGoBoot_SpringMVC] [nio-8880-exec-1

] s.w.s.m.m.a.RequestMappingHandlerMapping:

Mapped to

com.infosys.prs.controller.HomeController@getHomeDetails()

2024-12-07T18:22:16.578+05:30 DEBUG 5800 --- [DemoGoBoot_SpringMVC] [nio-8880-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver: Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9,/;q=0.8, application/signed-exchange;vb3;q=0.7]

2024-12-07T18:22:16.571+05:30 DEBUG 5800 {=}

[DemoGoBoot_SpringMVC] [nio-8888-exec-1] o.s.w.servlet.view.Internal ResourceView

View name [DemoGoHome], model

2024-12-07T18:22:16.574+05:38 DEBUG 5800

[

DemoGoBoot_SpringMVC] [nio-8080-exec-1] o.s.w.servlet.view.Internal ResourceView: Forwarding to [webapp/WEB-

INF/pages/DemoGoHome.jsp]

2024-12-07T18:22:16.587+05:30 DEBUG 5800 2024-12-07T18:22:16.590+05:30 DEBUG 5800 [DemoGoBoot_SpringMVC] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet [DemoGoBoot_SpringMVC] [nio-8880-exec-1] o.s.web.servlet.DispatcherServlet

: Completed 484 NOT FOUND : "ERROR" dispatch for GET

"/register/error", parameters={}

2024-12-07T18:22:16.594+05:30 DEBUG 5800 [DemoGoBoot_SpringMVC] [nio-8880-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping:

Mapped to

org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse) 2024-12-07T18:22:16.638+05:30 DEBUG 5880 --- [DemoGoBoot_SpringMVC] [nio-8088-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver:

Selected 'text/html" given

[text/html, text/html;q=0.8]

[DemoGoBoot_SpringMVC] [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet
: Exiting from "ERROR" 2024-12-07T18:22:16.653+05:30 DEBUG 5880 dispatch, status 484


Подробнее здесь: https://stackoverflow.com/questions/792 ... pring-boot
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»