Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between use @RequestMapping with and without method

I'm learning about Spring and MVC.

So, in the controller class, I have this method:

@RequestMapping(value="/buscaUsuario/{apodo}", method= RequestMethod.GET)
public String searchUser(@PathVariable("apodo") String apodo){
    String res;
    int usrId = this.usuarioService.bucarUsuario(apodo);        
    if(usrId == 0) res = "/error";
    else res =("/user/"+Integer.toString(usrId));
    return ("redirect:"+res);
}

And it works. But if I change it deleting the "method=RequestMethod.GET" part. I mean, using it like this:

@RequestMapping(value="/buscaUsuario/{apodo}")
public String searchUser(@PathVariable("apodo") String apodo){
    String res;
    int usrId = this.usuarioService.bucarUsuario(apodo);        
    if(usrId == 0) res = "/error";
    else res =("/user/"+Integer.toString(usrId));
    return ("redirect:"+res);
}

It ALSO works. So, my question is What's the difference?

like image 344
Argos Avatar asked Jul 15 '26 06:07

Argos


1 Answers

The @RequestMapping annotation handles all types of incoming HTTP request including GET, POST, PUT etc. By default, it’s assumed all incoming requests to URLs are of the HTTP GET kind. To differentiate the mapping by HTTP request type, you need to explicitly specify the HTTP request method. for more information

like image 105
Boldbayar Avatar answered Jul 18 '26 11:07

Boldbayar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!