Jersey Client将content-type标头设置为text / plain

时间:2014-03-12 08:39:59

标签: java jersey

使用泽西客户端发送HTTP请求。 Content-Type标头自动设置为" application / json" (作为一种性质),但我想改变内容类型"标题为" text / plain"无论任何规格,标准等,泽西岛版本为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 ***///

2 个答案:

答案 0 :(得分:3)

而不是

request.post(json, String.class);

尝试使用

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

答案 1 :(得分:3)

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