Java Jersey 客户端将内容类型标题设置为 text/plain

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22346056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:05:15  来源:igfitidea点击:

Jersey Client set content-type header as text/plain

javajersey

提问by Erdin? Ta?k?n

Using jersey client sending HTTP request. Content-Type header is automatically set as "application/json" (as a nature), but i want to changing "content-type" header with "text/plain" regardless of any specification, standards etc. Jersey version is 2.4.1.

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

Code

代码

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);

Request

要求

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 ***///

采纳答案by slartidan

instead of

代替

request.post(json, String.class);

try to use

尝试使用

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

回答by Oleg Estekhin

Use Entity.text(entityData)or Entity.entity(entityData, mediaType)methods instead of Entity.json()in your example.

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