diff --git a/spring-rest-http/src/main/java/com/baeldung/jsonparam/JsonParamController.java b/spring-rest-http/src/main/java/com/baeldung/jsonparam/JsonParamController.java index 60797cf104..d2a09296c2 100644 --- a/spring-rest-http/src/main/java/com/baeldung/jsonparam/JsonParamController.java +++ b/spring-rest-http/src/main/java/com/baeldung/jsonparam/JsonParamController.java @@ -1,19 +1,20 @@ package com.baeldung.jsonparam; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.databind.JsonMappingException; +import com.baeldung.controllers.DeferredResultController; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import java.io.IOException; - @Controller @RestController @RequestMapping("/api") public class JsonParamController { + private final static Logger LOG = LoggerFactory.getLogger(DeferredResultController.class); - @GetMapping(value = "/something") + @GetMapping(value = "/tickets") public String testQueryParamApi(@RequestParam("params") String params) { // params={"type":"foo","color":"green"} ParamObjectDTO paramObjectDTO; @@ -22,18 +23,28 @@ public class JsonParamController { paramObjectDTO = objectMapper.readValue(params, ParamObjectDTO.class); System.out.println(paramObjectDTO); // use paramObjectDTO where you have {"type":"foo","color":"green"} JSON data as Object - } catch (JsonParseException e) { - e.printStackTrace(); - } catch (JsonMappingException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + } catch (JsonProcessingException e) { + LOG.info("Json Processing Exception"); } return params; } + @GetMapping(value = "/tickets2") + public String testGetBodyParamApi(@RequestBody String params) { + // params={"type":"foo","color":"green"} + ParamObjectDTO paramObjectDTO; + ObjectMapper objectMapper = new ObjectMapper(); + try { + paramObjectDTO = objectMapper.readValue(params, ParamObjectDTO.class); + System.out.println(paramObjectDTO); + // use paramObjectDTO where you have {"type":"foo","color":"green"} JSON data as Object + } catch (JsonProcessingException e) { + LOG.info("Json Processing Exception"); + } + return params; + } - @PostMapping(value = "/something") + @PostMapping(value = "/tickets") public String testBodyParamApi(@RequestBody String params) { // params={"type":"foo","color":"green"} ParamObjectDTO paramObjectDTO; @@ -42,12 +53,8 @@ public class JsonParamController { paramObjectDTO = objectMapper.readValue(params, ParamObjectDTO.class); System.out.println(paramObjectDTO); // use paramObjectDTO where you have {"type":"foo","color":"green"} JSON data as Object - } catch (JsonParseException e) { - e.printStackTrace(); - } catch (JsonMappingException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + } catch (JsonProcessingException e) { + LOG.info("Json Processing Exception"); } return params; }