2013-08-28 27 views
0

我正在休息Web服务,并且正在通过FF中的Rest Client执行它。截至目前,它被用来作为后法现在我已经改变它的输入类型到JSON即现在我发送JSON输入到WS但其余客户端上我得到将休息Web服务的输入类型设置为Json

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

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

其余客户端上我已经设置页眉Content-Type application/json

以下是JSON输入我试图发送

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

任何人都可以指出我的错误或为我提出解决方案来实现这一点。

回答

0

我觉得这个代码可以帮助您:

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

客户

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。此代码为我工作。