Issue
the second day I can not solve the problem related to the 404th status. I watched articles about this problem, most of them had a problem with the folder hierarchy, but everything seems to be fine with me :'(
Folder sctructure
href="https://i.stack.imgur.com/N7rUE.png" rel="nofollow noreferrer">image
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>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.training</groupId>
<artifactId>regplustext</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>regplustext</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
UserRestController
package com.training.regplustext.controller;
import com.training.regplustext.entity.UserRegistrationEntity;
import com.training.regplustext.exception.AccountNotFound;
import com.training.regplustext.exception.NullRegistrationException;
import com.training.regplustext.service.UserService;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/welcome-page/")
@NoArgsConstructor
public class UserRestController {
private UserService userService;
@Autowired
public UserRestController(UserService userService){
this.userService = userService;
}
@PostMapping("registration-page/")
public ResponseEntity<String> regPage(@RequestBody UserRegistrationEntity newUser, @RequestParam Long id) {
try{
userService.registration(newUser.getUsername(), newUser.getPassword());
return ResponseEntity.ok("{id} id registration has passed successfully!");
}catch (NullRegistrationException ex){
return ResponseEntity.badRequest().body(ex.getMessage());
}
}
@PostMapping("login-page/")
public ResponseEntity<String> loginPage(@RequestBody UserRegistrationEntity user, @PathVariable Long id){
try{
userService.login(user.getUsername(), user.getPassword());
return ResponseEntity.ok("{id} id login has passed successfully!");
}catch (AccountNotFound ex){
return ResponseEntity.badRequest().body(ex.getMessage());
}
}
}
UserRegistrationEntity
package com.training.regplustext.entity;
import lombok.Data;
import javax.persistence.*;
@Entity(name = "UserRegistrationEntity")
@Table(name = "user_registration_table")
@Data
public class UserRegistrationEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(
name = "id",
nullable = false
)
private Long id;
@Column(
name = "username",
nullable = false
)
private String username;
@Column(
name = "password",
nullable = false
)
private String password;
}
UserRepository
package com.training.regplustext.repository;
import com.training.regplustext.entity.UserRegistrationEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface UserRepository extends JpaRepository<UserRegistrationEntity, Long> {
Optional<UserRegistrationEntity>findUserRegistrationEntityByUsernameAndPassword(String username, String password);
}
UserService
package com.training.regplustext.service;
import com.training.regplustext.entity.UserRegistrationEntity;
import com.training.regplustext.exception.AccountNotFound;
import com.training.regplustext.exception.NullRegistrationException;
public interface UserService {
UserRegistrationEntity registration(String username, String password) throws NullRegistrationException;
UserRegistrationEntity login(String username, String password) throws AccountNotFound;
}
UserServiceImpl
package com.training.regplustext.service;
import com.training.regplustext.entity.UserRegistrationEntity;
import com.training.regplustext.exception.AccountNotFound;
import com.training.regplustext.exception.NullRegistrationException;
import com.training.regplustext.repository.UserRepository;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@NoArgsConstructor
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
@Autowired
public UserServiceImpl(UserRepository userRepository){
this.userRepository = userRepository;
}
@Override
public UserRegistrationEntity registration(String username, String password) throws NullRegistrationException{
if(username == null || password == null)
throw new NullRegistrationException("username or password is nullable");
else{
UserRegistrationEntity newUser = new UserRegistrationEntity();
newUser.setUsername(username);
newUser.setPassword(password);
return userRepository.save(newUser);
}
}
@Override
public UserRegistrationEntity login(String username, String password) throws AccountNotFound{
if(userRepository.findUserRegistrationEntityByUsernameAndPassword(
username,
password
).isEmpty())
throw new AccountNotFound("account not found, please register first");
else{
return userRepository.findUserRegistrationEntityByUsernameAndPassword(
username,
password
).orElse(null);
}
}
}
RegplustextApplication
package com.training.regplustext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.training.regplustext.repository")
public class RegplustextApplication {
public static void main(String[] args) {
SpringApplication.run(RegplustextApplication.class, args);
}
}
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/reg_plus_text
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=admin
server.port=8081
postman
Solution
Can you check the @ComponentScan("com.training.regplustext.repository") path?
It should include all Component's like @Component, @Controller, @Service, @Repository, @Configuration, @RestController.
Please change it to @ComponentScan("com.training.regplustext").
Answered By - P S
Answer Checked By - David Marino (JavaFixing Volunteer)