我在 Postman 上使用 Spring Boot CRUD 登录和注册应用程序在 POST 请求中找不到错误 404

问题描述 投票:0回答:2

我在 spring boot 上创建了一个用户登录和注册应用程序,它成功运行没有任何错误,但是当我在 POSTMAN 上执行 POST 请求时显示错误“404 Not Found”。为什么会这样,下面分享我的代码-

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);
}

用户.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;
    }
}

用户服务.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;
    }
}

分享 POSTMAN 截图: enter image description here

java spring-boot postman crud registration
2个回答
0
投票

我猜你的代码很好,但我不确定你的 POSTMAN 请求参数。

要查看我是否正确,您应该在您的方法

registerUser()
中添加一个断点
RegistrationController 
只是为了查看当您通过POSTMAN调用此入口点时是否触发了断点。如果没有触发断点,说明你的POSTMAN请求没有写好


0
投票

错误是因为在 application.properties 中添加了上下文路径,因此邮递员无法在“http://localhost:8080/”找到我的应用程序。

我添加的上下文路径是:"server.servlet.context-path=/springboot-crud-rest";因此,我的应用程序运行在“http://localhost:8080/springboot-crud-rest”,POST 请求的 url 将是“http://localhost:8080/springboot-crud-rest/api/register”。

要解决此问题,请通过如上所述添加上下文路径来附加 url,或者从 application.properties 中删除上下文路径并仅使用较早的 url。

© www.soinside.com 2019 - 2024. All rights reserved.