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

时间:2017-08-28 09:28:48

标签: java spring

我使用spring MVC编写了以下控制器。 请求URL / webbroker /被映射到请求GET和POST。为了使其工作正常,但对于POST,它抛出以下错误。

  

请求方法不支持GET。

知道为什么它的表现如此?

@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);
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

@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方法复制处理程序方法。只需将method param声明为数组。