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

标签 java rest jersey

我使用的是 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/

相关文章:

java - 如何对十六进制字符串进行base-64编码

java - 在Java中实现Money类有哪些方法?

java - REST API 自动连接服务的远程或本地实现

java - 如何让jersey中的RESTful资源支持HTTPS

java - 尝试使用 Jersey 创建 REst 服务

java - 哈希表值不增加

java - 如何将 build.xml 中定义的变量值传递给 Java 类

java - @Transactional 大大减慢了 Rest API

javascript - 网络应用程序 : What should I send back to the user?

java - 在 Jersey 1.19.1 上禁用 WADL 生成