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

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

    In case you are using JAX RS, there is a method in javax.ws.rs.core.Response which automatically converts relative URLs:

    public static Response.ResponseBuilder created(java.net.URI location)

    Create a new ResponseBuilder for a created resource, set the location header using the supplied value.

    Parameters:

    • location - the URI of the new resource. If a relative URI is supplied it will be converted into an absolute URI by resolving it relative to the request URI.

    Note however that there is a bug in the JAX RS implementation CXF which leads to incorrect absolute URLs.

提交回复
热议问题