请求方法'GET'不受支持但它实际上存在于Controller中

时间:2016-02-08 16:30:09

标签: java spring spring-mvc

当我遇到这个奇怪的错误时,我正在开发一个简单的CRUD应用程序。很奇怪,因为在我的控制器类中,方便的@RequestMapping带注释的方法与请求GET方法映射一起出现。请求的URI为[context]/purchase/change/2。错误如下:

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

并且有我的控制器:

@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());
        purchasesDAO.updatePurchase(purchase);
        return "redirect:/user/" + userId;
    }

1 个答案:

答案 0 :(得分:2)

您已使用${...}语法创建了路径变量:

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

改为使用正确的{...}语法:

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