Response.header 是否覆盖@Produces 注释?

Does Response.header override the @Produces annotation?

我正在使用 Jersey 1.8,并且遇到 @Produces 注释与 Response.header()

不同的情况
@GET
@Produces(MediaType.TEXT_HTML)
public Response test(){
    return Response.status(200).header("Content-Type", "application/json;charset=utf-8")
   .entity("test response");
}

在这种情况下,响应内容类型是 html 还是 json?

我做了一个Jax-RS小程序来验证,似乎Response object中设置的header覆盖了@Produces注释。

您可以使用命令行实用程序对其进行测试 curl :

$ curl -s -i  http://localhost:8080/myapp/myresource/hello
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
Content-Length: 21

Hello World with JSON

我的 WS 与您的 WS 相似:

@GET
@Path("/hello")
@Produces(MediaType.TEXT_HTML)
public Response helloWorld() {
    return Response.status(200).header("Content-Type", "application/json;charset=utf-8")
            .entity("Hello World with JSON").build();
}