gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:19:57 24 4
gpt4 key购买 nike

我使用的是 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 对象中设置的 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();
}

关于java - Response.header 是否会覆盖 @Produces 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50004932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com