What's the proper way to set the Location header for an HTTP 201 response in a Java Servlet application

前端 未结 5 1500
醉梦人生
醉梦人生 2021-01-31 02:49

Consider the following code sending an HTTP 201 \"Created\" response to the client:

    String url = \"/app/things?id=42\"; // example
    response.setStatus(Htt         


        
5条回答
  •  长发绾君心
    2021-01-31 03:19

    Unfortunately, the servlet API does not provide a method which directly returns the absolute URL up to with the context root. For that I have several times had to use a combination of getRequestURL(), getRequestURI() and getContextPath().

    String absoluteContextRootURL = request.getRequestURL().toString().replace(request.getRequestURI().substring(1), request.getContextPath());
    

提交回复
热议问题