Jersey Client导致415不支持的媒体类型 [英] Jersey Client resulting in 415 Unsupported media type

查看:150
本文介绍了Jersey Client导致415不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

明显的原因是没有提供适当的内容类型.但我提供.我仍然收到不受支持的媒体类型.不知道为什么.任何帮助都将不胜感激.

The obvious reason for this would be not providing proper content type. But I am providing. Still i am getting Unsupported Media Type. Not sure why. Any help greatly appreciated.

Client c = Client.create();
WebResource resource = c.resource(HOST+"/test");

Gson gson = new Gson();
Test test = new Test();
test.setTestName("TEST AUTOMATION");

resource.header("Content-Type", "Application/json");

String testStr = gson.toJson(test);
System.out.println("Request Str: "+testStr);
ClientResponse response = resource.post(ClientResponse.class, testStr);
System.out.println("POST response : "+response);
POST response : POST http://host:8888/test returned a response status of 415 Unsupported `enter code here`Media Type

推荐答案

这是我解决的方法.它真的很奇怪.直到我合并以下语句,它才起作用.从我编写的上述程序中,将header语句和post语句组合如下.同样不要忘记将charset = UTF-8放进去.

This is how i solved it. Its really weird. Until i combine the statements as below, it didn't work. From the above program that i wrote, combine the header statement and post statement as below. Also don't forget to put charset=UTF-8.

ClientResponse response = resource.header("Content-Type",
            "application/json;charset=UTF-8").post(ClientResponse.class,
            testStr);

这篇关于Jersey Client导致415不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆