Jersey Client resulting in 415 Unsupported media type

柔情痞子 提交于 2019-12-11 14:04:00

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/26104101/jersey-client-resulting-in-415-unsupported-media-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!