I made a user login & registration app on spring boot, it runs successfully without any error but displays the error "404 Not Found" when I do a POST request on POSTMAN. Why is it so, sharing my code below-
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.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.guides.springboot3</groupId>
<artifactId>Backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Backend</name>
<description>Spring Boot Backend</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>19</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>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
RegistrationController.java
package net.guides.springboot3.Backend.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestParam;
//import net.guides.springboot3.Backend.exception.ResourceNotFoundException;
import net.guides.springboot3.Backend.model.User;
import net.guides.springboot3.Backend.repository.UserRepository;
import net.guides.springboot3.Backend.service.UserService;
@RestController
@RequestMapping("/api")
public class RegistrationController {
private UserService userService;
public RegistrationController(UserService userService) {
this.userService = userService;
}
@PostMapping("/register")
public ResponseEntity<Object> registerUser(@Valid @RequestBody User user) {
boolean isRegistered = userService.registerUser(user);
if (!isRegistered) {
return new ResponseEntity<>("Email already in use", HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>("User registered successfully. Verify your email before login", HttpStatus.OK);
}
//verifyEmail endpoint method
@GetMapping("/verify-email")
public ResponseEntity<Object> verifyEmail(@RequestParam("token") String token) {
boolean isVerified = userService.verifyEmail(token);
if (!isVerified) {
return new ResponseEntity<>("Invalid token", HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>("Email verified successfully", HttpStatus.OK);
}
}
UserRepository.java:
package net.guides.springboot3.Backend.repository;
import net.guides.springboot3.Backend.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
boolean existsByEmail(String email);
User findByVerificationToken(String token);
}
User.java:
package net.guides.springboot3.Backend.model;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.GeneratedValue;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "email", unique = true, nullable = false)
private String email;
@Column(name = "password", nullable = false)
private String password;
private String verificationToken;
// other fields
// getters and setters
public String getVerificationToken() {
return verificationToken;
}
// getters and setters
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
@Column(name = "verification_token")
public void setVerificationToken(String verificationToken) {
this.verificationToken = verificationToken;
}
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "is_verified", nullable = false)
private boolean isVerified;
// getters and setters
public void setIsVerified(boolean isVerified) {
this.isVerified = isVerified;
}
public boolean getIsVerified() {
return isVerified;
}
// getters and setters
public String getEmail() {
return email;
}
}
UserService.java:
package net.guides.springboot3.Backend.service;
import java.util.UUID;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import net.guides.springboot3.Backend.model.User;
import net.guides.springboot3.Backend.repository.UserRepository;
import net.guides.springboot3.Backend.model.User;
import net.guides.springboot3.Backend.repository.UserRepository;
@Service
public class UserService {
private BCryptPasswordEncoder passwordEncoder;
private UserRepository userRepository;
private JavaMailSender javaMailSender;
public UserService(UserRepository userRepository, JavaMailSender javaMailSender) {
this.userRepository = userRepository;
this.javaMailSender = javaMailSender;
this.passwordEncoder = new BCryptPasswordEncoder();
}
public boolean registerUser(User user) {
if (userRepository.existsByEmail(user.getEmail())) {
return false;
}
//Encrypt password using bcrypt
user.setPassword(passwordEncoder.encode(user.getPassword()));
user.setVerificationToken(UUID.randomUUID().toString());
user.setIsVerified(false);
userRepository.save(user);
// send verification email
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(user.getEmail());
mailMessage.setSubject("Verify your email address");
mailMessage.setText("Please click the link below to verify your email address: \n"
+ "http://localhost:8080/verify-email?token=" + user.getVerificationToken());
javaMailSender.send(mailMessage);
return true;
}
public boolean verifyEmail(String token) {
User user = userRepository.findByVerificationToken(token);
if (user == null) {
return false;
}
user.setIsVerified(true);
user.setVerificationToken(null);
userRepository.save(user);
return true;
}
}
Sharing the POSTMAN screen snap: enter image description here
The error was because of adding a context path in application.properties, due to which postman was unable to find my application at 'http://localhost:8080/'.
The context path I added was: "server.servlet.context-path=/springboot-crud-rest" ; hence my application was running at 'http://localhost:8080/springboot-crud-rest' & the url for POST request would be 'http://localhost:8080/springboot-crud-rest/api/register'.
To resolve this, either append the url by adding context path as above or remove the context path from application.properties & use the earlier url only.