Kavv February 2016

Request method 'GET' not supported but it's actually present in Controller

I was developing a simple CRUD application, when i've encountered a this weird error. Weird, because in my controller class convenient @RequestMapping annotated method is present with request GET method mapping. The requested URI is [context]/purchase/change/2. The error is following:

org.springframework.web.servlet.PageNotFound - Request method 'GET' not supported

and there is my controller:

@Controller
@RequestMapping("/purchase")
public class PurchasesController {

    //...

    @RequestMapping(value = "/add/{userId}", method = RequestMethod.GET)
    public String addPurchase(Model model, @PathVariable int userId) {
        //that method works with mapping ex. "context/purchase/add/1"
        return "purchase_update_add";
    }

    @RequestMapping(value = "/add/{userId}", method = RequestMethod.POST)
    public String addPurchase(
            @ModelAttribute("purchase") PurchaseDTO purchaseDto,
            @PathVariable int userId) {

        //that works too

        return "redirect:/user/" + userId;
    }

    @RequestMapping(value = "/change/${purchaseId}", method = RequestMethod.GET)
    public String changePurchaseDate(Model model, @PathVariable int purchaseId) {

        model.addAttribute("operation", "change");

        PurchaseDTO purchase = new PurchaseDTO();
        Purchase purchaseEntity = purchasesDAO.getPurchase(purchaseId);
        purchase.setDate(purchaseEntity.getDate());
        purchase.setId(purchaseId);

        model.addAttribute("purchase", purchase);

        return "purchase_update_add";
    }

    @RequestMapping(value = "/change/{userId}", method = RequestMethod.POST)
    public String changePurchaseDate(
            @ModelAttribute("purchase") PurchaseDTO purchaseDto,
            @PathVariable int userId) {
        Purchase purchase = purchasesDAO.getPurchase(purchaseDto.getId());
        purchase.setDate(purchaseDto.getDate());
        pur        

Answers


Ali Dehghani February 2016

You've created a Path Variable with ${...} syntax:

@RequestMapping(value = "/change/${purchaseId}", method = RequestMethod.GET)
                                 ^ $ is redundant

Use the correct {...} syntax instead:

@RequestMapping(value = "/change/{purchaseId}", method = RequestMethod.GET)

Post Status

Asked in February 2016
Viewed 3,450 times
Voted 10
Answered 1 times

Search




Leave an answer