简体   繁体   中英

Jersey Client resulting in 415 Unsupported media type

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM