首页 文章

将其余Web服务的输入类型设置为Json

提问于
浏览
0

我正在使用休息Web服务,我正在通过FF中的Rest Client执行它 . 直到现在它被用作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

下面是ws的新签名(我添加了Consume and Produce Line)

@Override
    @POST
    @Path("/addHouseHoldAccounts")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response addHouseHoldAccounts(JSONObject jsonObject) {
....
....
}

在休息客户端我已设置 Headers Content-Type application/json

以下是我正在尝试发送的JSON输入

{
   "jsonObject":{
       "JsonId":"17fb00b6-dfa3-4cc6-b7ba-c54ecd429350",
   "JsonTypeOf":"JSonTest",
   "JsonType":"",
   "JsonTypetName":"JsonImplemented"
   }
}

任何人都可以指出我的错误或建议我实现这一目标的解决方案 .

2 回答

  • 0

    尝试将请求标头 "Accept" 设置为 "application/json"

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

  • 0

    我认为此代码可以帮助您:

    网络服务:

    @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;
            }
    

    客户

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

    我不确定,但我认为你没有将内容类型设置为json . 这段代码对我有用 .

相关问题