Java:如何从响应中删除标头?

时间:2018-01-30 14:00:43

标签: java response httpresponse

我有一个由ResponseBuilder创建的响应,如果它已经存在,则想从中删除一个标头,以确保我没有为同一个标头设置多个值(content-disposition)。

我该怎么做?

1 个答案:

答案 0 :(得分:3)

对于我的用例,我发现我可以首先将标头设置为null,然后设置新值来删除任何现有标头。

e.g。

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("response_test")
@GET
public Response response(){

    // Pretend we've got a response builder that was created by 
    // some code we don't control
    ResponseBuilder builder = Response.status(200);
    builder.entity("Test Me");
    builder.header("content-disposition", "attachment; filename=a.txt");

    // Now remove any "content-disposition" header that's there
    // and replace it with our updated header.
    builder.header("content-disposition", null);
    builder.header("content-disposition", "attachment; filename=b.txt");
    Response response = builder.build();
    return response;
}