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

前端 未结 5 1480
醉梦人生
醉梦人生 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:25

    You might try

    new URL(new URL(request.getRequestURL().toString()), url).toString();
    

    That will at least be smart about canonicalizing away any .. or other oddities. Other than that, I don't think it's much better than string manipulation.

提交回复
热议问题