admin管理员组

文章数量:1397732

I'm having trouble implementing a patch mapping in springboot. This is the structure of the entity I am attempting to update

@Entity
@Component
public class AppUser {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "user_sequence")
    @SequenceGenerator(name = "user_sequence", initialValue = 100, allocationSize = 1)
    private int id;
    private String username;
    private String password;
    private double balance;

    /* Getter and setter methods here */
}

Here is the patch mapping code

@PatchMapping("/api/user")
public ResponseStructure<AppUser> updateUser(@RequestBody AppUser user) {
    //ResponseStructure is only used to parse response data
    ResponseStructure<AppUser> responseStructure = new ResponseStructure<AppUser>();

    try {
        appUserRepository.save(user);

        responseStructure.setStatusCode(200);
        responseStructure.setMessage("User updated successfully");
        responseStructure.setData(user);

        return responseStructure;
    }
    catch (Exception exception) {
        responseStructure.setStatusCode(400);
        responseStructure.setMessage("Failed to update user: \n" + exception.getMessage());

        return responseStructure;
    }
}

Here is the body I use when sending the patch request and the subsequent response from Postman

{
    "id": 100,
    "username": "User3",
    "password": "Password1",
    "balance": 0.0
}
{
    "timestamp": "2025-03-26T23:16:47.331+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/api/user/"
}

I have personally verified that there is an AppUser with id 100 in my database. Why is it returning a 404 error?

本文标签: spring bootJpaRepository save method returning 404 errorStack Overflow