问题描述
下面是我的方法
@RequestMapping(value = "/authorise",method = {RequestMethod.POST,RequestMethod.GET})
public String authorise(@RequestParam(name = "checkout_token",required = false) final String checkoutToken,final Model model,final RedirectAttributes redirectModel) {
boolean success = false;
try
{
success = affirmPaymentFacade.authorisePayment(checkoutToken);
}
catch (RuntimeException re)
{
LOG.warn("error during payment authorisation ",re);
}
if(success){
final OrderData orderData;
try
{
orderData = getCheckoutFacade().placeOrder();
}
catch (final Exception e)
{
LOG.error("Failed to place Order",e);
//Todo-BE auth reversal
//GlobalMessages.addErrorMessage(model,"checkout.affirm.order.Failed");
return REDIRECT_PREFIX + CHECKOUT_AFFIRM_ERROR;
}
return redirectToOrderConfirmationPage(orderData);
}else {
//GlobalMessages.addErrorMessage(redirectModel,"checkout.affirm.authorisation.Failed");
return REDIRECT_PREFIX + CHECKOUT_AFFIRM_ERROR;
}
}
当调用GET请求时,它可以正常工作,但是当我们使用POST请求进行调用并请求有效负载时 checkout_token = XXXX,我收到以下错误消息
WARN [hybrisHTTP24] [DefaultHandlerExceptionResolver]已解决 [org.springframework.web.HttpRequestMethodNotSupportedException: 请求方法'POST'不支持]
编辑
即使我尝试删除所有参数,但到目前为止仍然没有运气。
解决方法
首先,有关在StackOverflow上进行更好的发布的一些信息:
- 将问题简化为尽可能紧凑的代码(如果您在路由至方法时遇到问题,则方法中的代码无关紧要)
- 这不是唯一的混杂问题,因为使用了Spring MVC。在Spring MVC论坛中,就像在Hybris stackoverflow中一样,您将获得更多答案。
- 请包括您的进口商品
使用以下代码:
package com.example.demo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@RestController
public class TestController
{
@RequestMapping(value = "/authorise",method = { RequestMethod.POST,RequestMethod.GET })
public String authorise(@RequestParam(name = "checkout_token",required = false) final String checkoutToken,final Model model,final RedirectAttributes redirectModel)
{
return "authorise";
}
}
}
该方法可通过GET和POST访问:
curl -X GET http://localhost:9080/authorise
>> authorise
curl -X POST http://localhost:9080/authorise
>> authorise
也许您正在尝试使用错误的网址?
,尝试为此网址禁用csrf令牌