Home > Enterprise >  How to resolve [org.springframework.web.HttpRequestMethodNotSupportedException: Request method '
How to resolve [org.springframework.web.HttpRequestMethodNotSupportedException: Request method '

Time:11-30

I am trying to Get, Delete and Post users' details. I tried to send a request using Postman It showed 401 Unauthorized status. I have tried every possible solution but none of them worked. I am getting this error on the browser -

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Mon Nov 29 14:28:46 IST 2021 There was an unexpected error (type=Method Not Allowed, status=405).

UserController.java

    @RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // create user
    @RequestMapping(value ="/" , method = RequestMethod.POST)
    public Users createUser(@RequestBody Users users) throws Exception {

        Set<UserRole>  userroles = new HashSet<>();

        Role role = new Role();
        role.setRoleName("Student");
        role.setRoleID(23L);

        UserRole userRole = new UserRole();
        userRole.setUsers(users);
        userRole.setRoles(role);

        userroles.add(userRole);

    return this.userService.createUser(users, userroles);

    }


     @GetMapping("/{username}")
    //@RequestMapping(value ="/{username}" , method = RequestMethod.GET)
    public Users getUser(@PathVariable("username") String username){

        return  this.userService.getUser(username);
    }

   // @DeleteMapping("/{userId}")
   @RequestMapping(value ="/{userId}" , method = RequestMethod.DELETE)
    public void deleteUser(@PathVariable("userId") Long userId){

        this.userService.deleteUser(userId);
    }

UserServiceImplementation.java

@Service
public class  UserServiceImplementation implements UserService {

    // To save user
    @Autowired
    private UserRepository userRepository;

    // To save role
    @Autowired
    private RoleRepository roleRepository;

    @Override
    public Users createUser(Users users, Set<UserRole> userRoles) throws Exception {

        // check whether user is already on database
      Users registered = this.userRepository.findByUsername(users.getUsername());

      // if registered
      if (registered != null){
          System.out.println("User is already registered !");
      throw new Exception("User is already present !");
      }
      // if not registered
      else{

          // create user
          // user can have many roles so use for loop
         // access each role one by one
          for (UserRole ur : userRoles){
              // save all roles in the role repo
              roleRepository.save(ur.getRoles());
          }

          // assign all roles to the user
          users.getUserRoles().addAll(userRoles);
          registered = this.userRepository.save(users);

      }

        return registered;
    }

    // getting user by username
    @Override
    public Users getUser(String username) {
        return this.userRepository.findByUsername(username);
    }

    @Override
    public void deleteUser(Long userId){
        this.userRepository.deleteById(userId);
    }

}

CodePudding user response:

Try to remove @RequestMapping("/user")

CodePudding user response:

Instead of

@RequestMapping(value ="/" , method = RequestMethod.POST)

use

@PostMapping(value = "/")

CodePudding user response:

type=Method Not Allowed This comes when you are tryong to access endpoints in a wrong way from postman

Eg: using GET for a post endpoint etc, Can you please check

  • Related