Whitelabel Error Page. Find and fix this. This application has no explicit mapping for…
- User
Whitelabel Error Page. Find and fix this. This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Internal Server Error, status=500). File UserController: import net.crud.springbootcrud.model.User; import net.crud.springbootcrud.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller public class UserController { private final UserService userService;
public UserController(UserService userService) { this.userService = userService; }
@GetMapping(“/users”) public String findAll(Model model){ List users = userService.findAll(); model.addAttribute(“users”,users); return “user-list”; } @GetMapping(“/user-create”) public String createUserForm(User user){ return “user-create”; } @PostMapping(“/user-create”) public String createUser(User user){ userService.saveUser(user); return “redirect:/users”; } File UserService: import net.crud.springbootcrud.model.User; import net.crud.springbootcrud.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List; import java.util.Optional;
@Service public class UserService { private final UserRepository userRepository;
public UserService(UserRepository userRepository) { this.userRepository = userRepository; }
public User findById(Long id){ Optional userOptional = userRepository.findById(id); if(userOptional.isPresent()){ return userOptional.get(); } throw new IllegalArgumentException(“User with id " + id + " doesn’t exist”); } public List findAll(){ return userRepository.findAll(); } public User saveUser(User user){ if(user.getUsername() == null || user.getUsername().isEmpty()){ throw new IllegalArgumentException(“Username can not be empty”); } return userRepository.save(user); } public void deleteById(Long id){ Optional userOptional = userRepository.findById(id); if(userOptional.isPresent()){ userRepository.deleteById(id); } else{ throw new IllegalArgumentException(“User with id " + id + " doesn’t exist”); } } } File springbootcrudApllictation: import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class SpringbootcrudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootcrudApplication.class, args); }
Interface UserRepository: import net.crud.springbootcrud.model.User; import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> { File application.properties:spring.datasource.url=jdbc:mysql://localhost:3306/springbootcrud spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=7788 server.port=8050
- Assistant
The error seems to be a Whitelabel Error Page, which means that the web application encountered an unexpected error and does not have an explicit page mapping for displaying this error. To fix this, you can create a custom error page and configure it in your application's web configuration. Here's an example of how to do it in Spring Boot:
-
Create a new HTML page that will be used as your custom error page. Name it
error.htmland save it in thesrc/main/resources/templatesdirectory. -
Add the following code to your application's web configuration file (
SpringbootcrudApplication.java):
import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/error") public class CustomErrorController implements ErrorController { @RequestMapping public String handleError() { // Handle the error and return the path to your custom error page return "error"; } @Override public String getErrorPath() { return "/error"; } } -