gpt4 book ai didi

java - get 和 post 的相同请求映射不适用于 spring

转载 作者:行者123 更新时间:2023-11-30 06:31:57 25 4
gpt4 key购买 nike

我使用 spring MVC 编写了以下 Controller 。请求 URL/webbroker/映射到请求 GET 和 POST。对于 get 其工作正常,但对于 POST 则抛出以下错误。

Request method GET not supported.

知道为什么它会这样吗?

@Controller
public class ProxyController {

@Autowired
private RequestHandler reqHandler;

@Autowired
private ResponseHandler responseHandler;


@RequestMapping(value = "/webbroker/**", method = RequestMethod.GET)
public void edgefxGetRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
throws IOException {
HttpURLConnection connection = this.reqHandler.handleRequest(httpRequest, httpResponse, false);
this.responseHandler.sendResponse(connection, httpResponse);
}

@RequestMapping(value = "/webbroker/**", method = RequestMethod.POST)
public void edgefxPostRequest(HttpServletRequest httpRequest,
HttpServletResponse httpResponse) throws IOException, URISyntaxException {
HttpURLConnection connection = this.reqHandler.handleRequest(httpRequest, httpResponse, true);
this.responseHandler.sendResponse(connection, httpResponse);
}

@RequestMapping(value = "/webbroker-strong/**", method = RequestMethod.GET)
public void edgefxStrongGetRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
throws IOException {
HttpURLConnection connection = this.reqHandler.handleRequest( httpRequest, httpResponse, false);
this.responseHandler.sendResponse(connection, httpResponse);
}

@RequestMapping(value = "/webbroker-strong/**", method = RequestMethod.POST)
public void edgefxStrongPostRequest(HttpServletRequest httpRequest,
HttpServletResponse httpResponse) throws IOException, URISyntaxException {
HttpURLConnection connection = this.reqHandler.handleRequest(httpRequest, httpResponse, true);
this.responseHandler.sendResponse(connection, httpResponse);
}
}

最佳答案

试试这个:

@RequestMapping(value = "/webbroker/**", method = { RequestMethod.GET, RequestMethod.POST })
public void edgefxRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
throws IOException {
HttpURLConnection connection = this.reqHandler.handleRequest(httpRequest, httpResponse, false);
this.responseHandler.sendResponse(connection, httpResponse);
}

您不需要为这两种 HTTP 方法复制处理程序方法。只需将方法参数声明为数组即可。

关于java - get 和 post 的相同请求映射不适用于 spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45915828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com