2015-09-09 24 views
1

我的控制器應該只消費JSON。我正在使用的數據結構是未知的,所以我無法將它映射到一個對象。現在我只想要簡單的JSON字符串並記錄下來。最好只有最大值。 1000的長度接受,所以這不能用於胸圍記憶。消費JSON - 只獲取字符串

我該如何做到這一點?

@RequestMapping(value = "/consume/everything/", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) 
public void consumeEverything(final HttpServletRequest request, final HttpServletResponse response) { 
// ... 
String jsonString = ...; 
LOG.info("jsonString = {}", jsonString); 
} 

回答

1

可以使用@RequestBody註釋(見reference documentation)。

@RequestMapping(value = "/consume/everything/", 
    method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) 
public void consumeEverything(@RequestBody String body) { 
+0

最明顯的事情是最難的。謝謝! – user871611