1. 程式人生 > >HTTP method POST is not supported by this URL解決

HTTP method POST is not supported by this URL解決

ons ssa cte buffered class over err inpu public

今天寫了個非常簡單的setvlet想測試些東西,寫好了後用postman請求報錯,報錯內容如下

{
    "timestamp": 1504170113588,
    "status": 405,
    "error": "Method Not Allowed",
    "message": "HTTP method POST is not supported by this URL",
    "path": "/vincent/aaa"
}

但是我的控制臺還是答應了我的請求內容,百思不得其解,最後發現是因為沒有把調用父類的post方法刪掉

我的代碼如下

@WebServlet(urlPatterns = "
/aaa") public class MyServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException { super.doGet(request, resp); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException { InputStream inputStream
= request.getInputStream(); BufferedReader in=new BufferedReader(new InputStreamReader(request.getInputStream())); StringBuilder sb = new StringBuilder(); String xmlHead = ""; String xmlContent=""; String line = null; while ((line = in.readLine()) != null
) { sb.append(line); } System.out.println(sb); super.doPost(request, resp); } }
 super.doPost(request, resp);的源碼如下
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_post_not_supported");
        if (protocol.endsWith("1.1")) {
            resp.sendError(405, msg);
        } else {
            resp.sendError(400, msg);
        }

    }

有沒有發現,無論你的請求怎樣,他都報錯的。

HTTP method POST is not supported by this URL解決