2017-01-30 54 views
0
發送JSON

我試圖發送JSON字符串到Spring控制器,我得到400 - 錯誤的請求作爲應答400(無效請求),而在春季

我使用彈簧4.0.3

這是我的控制器

@Controller 
public class Customer{ 

    @RequestMapping(value = "/apis/test", method = RequestMethod.GET, produces = "application/json") 
    public @ResponseBody String test(HttpServletRequest params) throws JsonIOException { 
     String json = params.getParameter("json"); 
     JsonParser jObj = new JsonParser(); 
     JsonArray jsonObj = (JsonArray) jObj.parse(json); 

     for(int i = 0; i < jsonObj.size(); i++) { 
      JsonObject jsonObject = jsonObj.get(i).getAsJsonObject(); 
      System.out.println(jsonObject.get("name").getAsString()); 

     } 
     return json; 
    } 
} 

請幫我解決這個

+0

我使用angular JS進行集成 –

+0

分享在瀏覽器中提出的請求。捕獲網絡標籤中的請求 – Barath

+0

我們需要知道您是如何呼叫此請求的......請分享詳情 – user2447161

回答

0
@RequestMapping(value = "/apis/test", method = RequestMethod.GET, produces = "application/json") 

上述MEA ns這是一種通常不接受數據的HTTP GET方法。您應該使用一個HTTP POST方法,如:

@RequestMapping(value = "/apis/test", method = RequestMethod.POST, consumes = "application/json") 
    public @ResponseBody String test(@RequestParam final String param1, @RequestParam final String param2, @RequestBody final String body) throws JsonIOException { 

則可以執行POST/API的/測試參數1 =一個&參數2 =兩個在請求

我希望的RequestBody添加字符串幫助!

+0

其實我想通過一些其他參數與JSON像https://s30.postimg.org/afd8s81kh/sc_2.png –

+0

我更新了答案,以顯示如何添加其他參數 – ozOli

+0

你能告訴我如何從POST方法回收數據?我正在嘗試使用HttpServletRequest參數,但它給出空值 –