【问题标题】:Jersey Client set content-type header as text/plainJersey 客户端将内容类型标头设置为 text/plain
【发布时间】:2014-04-16 06:21:33
【问题描述】:

使用 jersey 客户端发送 HTTP 请求。 Content-Type 标头自动设置为“application/json”(作为一种性质),但我想用“text/plain”更改“content-type”标头,不管任何规范、标准等。泽西版本是 2.4.1 .

代码

String target = "http://192.168.1.2:10000";
String path = "test3";

Client c = ClientBuilder.newClient ();
WebTarget target = c.target (target).path (path);

Entity<SubscriberBean> json = Entity.json (subscriber);
Builder request = target.request ();

String response = request.post(json, String.class);

请求

POST /test3 HTTP/1.1
Accept: text/html
Content-Type: application/json
User-Agent: Jersey/2.4.1 (HttpUrlConnection 1.6.0_17)
Host: 192.168.1.2:10000
Connection: keep-alive
Content-Length: 278

///**** Some json data ***///

【问题讨论】:

    标签: java jersey


    【解决方案1】:

    而不是

    request.post(json, String.class);
    

    尝试使用

    request.type(MediaType.TEXT_PLAIN).post(json, String.class);
    

    【讨论】:

      【解决方案2】:

      在您的示例中使用Entity.text(entityData)Entity.entity(entityData, mediaType) 方法而不是Entity.json()

      【讨论】:

        猜你喜欢
        • 2010-11-05
        • 2014-11-21
        • 2019-03-04
        • 1970-01-01
        • 1970-01-01
        • 2020-11-15
        • 1970-01-01
        • 1970-01-01
        • 2018-12-25
        相关资源
        最近更新 更多