Search code examples
springspring-mvcrequest-mappingspring-framework-beans

Use precise mapping annotation, i.e. '@GetMapping', '@PostMapping', etc


Replace @RequestMapping with specific @GetMapping, @PostMapping etc.

I'm trying to show the jsp page in the browser. So I used @RequestMapping. But it shows warning like Use precise mapping annotation, i.e. '@GetMapping', '@PostMapping', etc.

public class LocationControler 
{
    @RequestMapping("/showCreate")
    public String showCreate()
    {
        return "CreateLocation";
    }
}

CreateLocation will display the jsp file CreateLocation.jsp.

In application.properties I have set like this

spring.mvc.view.prefix=/WEB-INF/jsps/
spring.mvc.view.suffix=.jsp

I tried with @PostMapping In browser I'm getting this error. There was an unexpected error (type=Method Not Allowed, status=405).

For this whic method should I use?.

@GetMapping - shortcut for @RequestMapping(method = RequestMethod.GET)
@PostMapping - shortcut for @RequestMapping(method = RequestMethod.POST)
@PutMapping - shortcut for @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping - shortcut for @RequestMapping(method =RequestMethod.DELETE)

Solution

  • Unless you are using specific http method, go with @GetMapping, which will be used by default by your browser. I do recommend to read up about various http methods and their usage though.