简体   繁体   中英

Unable to do delete operation through spring rest endpoints

I am trying to create a crud application with spring as backend.I have created maaping for fetching,creating and deleting users.However the delet request is not working.The other two mapping work fine.

Here is my repository:

@Repository
public interface UsersRepository extends JpaRepository<Users,Long> {

    @Transactional
    void deleteByuserName(String username);
}

Here is my controller:

@RestController
public class UsersController {

    @Autowired
    UsersRepository usersRepository;

    @PostMapping(value = "/users/create")
    public Users postCustomer(@RequestBody Users user) {

        Users _user = usersRepository.save(new Users(user.getUserName(),user.getPassword(),user.getUserRole()));
        return _user;
    }


    @GetMapping("/users")
    public List<Users> getAllReports()
    {
        return usersRepository.findAll();
    }



    @DeleteMapping(value = "/users/delete/{userName}")
    public @ResponseBody void deleteCustomer(@PathVariable("userName") String userName) {
        System.out.println("Delete User with name = " + userName + "...");

        usersRepository.deleteByuserName(userName);
    }

}

The error I get in postman is:

{
    "timestamp": "2019-04-12T09:18:51.401+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/users/delete"
}

I might be wrong on this but I would expect the error in postman to be "path": "/users/delete/user"? Are you sure you're passing the username for the path variable?

Does the request ever enter the controller and print your message:

System.out.println("Delete User with name = " + userName + "...");

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM