I'm having a rest web service and i'm executing it through Rest Client in FF. Till now it was used as a post method now i've to change it's input type to JSON ie now i'm sending json input to that ws but on rest client i'm getting
Status Code: 415 Unsupported Media Type
Connection: Keep-Alive
Content-Length: 0
Content-Type: text/plain
Date: Wed, 28 Aug 2013 07:52:50 GMT
Keep-Alive: timeout=5, max=99
Server: Apache/2.2.19 (Win32) mod_jk/1.2.30 PHP/5.3.6
Below is the ws new signature (I've added the Consume and Produce Line)
@Override
@POST
@Path("/addHouseHoldAccounts")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response addHouseHoldAccounts(JSONObject jsonObject) {
....
....
}
on rest client i've set header Content-Type application/json
and below is the JSON input i'm trying to send
{
"jsonObject":{
"JsonId":"17fb00b6-dfa3-4cc6-b7ba-c54ecd429350",
"JsonTypeOf":"JSonTest",
"JsonType":"",
"JsonTypetName":"JsonImplemented"
}
}
can anyone point out my mistake or suggest me a solution for achieving this.
Try Setting the Request Header "Accept"
as "application/json"
I think this code can help you:
WebService:
@POST
@Path("/your_path")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
//Receive and send a json object
public JSONObject receiveJSONObject(JSONObject json) throws JSONException
{
return json;
}
Client
private void sendJSONObject() throws JSONException{
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource service = client.resource("*your_adress_and_path*");
JSONObject data= new JSONObject();
dados.put("Name", "John");
dados.put("Age", "30");
dados.put("City", "Tokyo");
ClientResponse client_response = service.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, data);
System.out.println("Status: "+client_response.getStatus());
client.destroy();
}
I'm not sure, but I think you aren't setting the content type as json. This code worked for me.
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.