简体   繁体   English

将Rest Web Service的输入类型设置为Json

[英]Set input type of a rest web service to Json

I'm having a rest web service and i'm executing it through Rest Client in FF. 我正在使用rest Web服务,并且正在通过FF中的Rest Client执行它。 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 直到现在它被用作post方法,现在我必须将其输入类型更改为JSON,即现在我将json输入发送到该ws,但在其余客户端上却得到了

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) 以下是ws的新签名(我已经添加了“消费和生产线”)

@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 在其余客户端上,我已设置标头Content-Type application/json

and below is the JSON input i'm trying to send 以下是我尝试发送的JSON输入

{
   "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" 尝试将请求标头"Accept""application/json"

http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z3 http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z3

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. 我不确定,但是我认为您没有将内容类型设置为json。 This code worked for me. 这段代码对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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