From 24833b7e92ef4511f50d94a5e4f865f5b60d3002 Mon Sep 17 00:00:00 2001 From: "alex.peptan" Date: Mon, 13 Apr 2020 17:23:23 +0300 Subject: [PATCH] BAEL-3896: OpenAPI JSON Objects in Query Params --- spring-rest-http/README.md | 1 + .../jsonparam/JsonParamController.java | 55 +++++++++++++++++++ .../baeldung/jsonparam/ParamObjectDTO.java | 33 +++++++++++ 3 files changed, 89 insertions(+) create mode 100644 spring-rest-http/src/main/java/com/baeldung/jsonparam/JsonParamController.java create mode 100644 spring-rest-http/src/main/java/com/baeldung/jsonparam/ParamObjectDTO.java diff --git a/spring-rest-http/README.md b/spring-rest-http/README.md index 35793cb281..cba017eb74 100644 --- a/spring-rest-http/README.md +++ b/spring-rest-http/README.md @@ -13,3 +13,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring RequestMapping](https://www.baeldung.com/spring-requestmapping) - [Guide to DeferredResult in Spring](https://www.baeldung.com/spring-deferred-result) - [Using JSON Patch in Spring REST APIs](https://www.baeldung.com/spring-rest-json-patch) +- [OpenAPI JSON Objects in Query Params](https://www.baeldung.com/openapi-json-objects-in-query-params) 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 new file mode 100644 index 0000000000..60797cf104 --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/jsonparam/JsonParamController.java @@ -0,0 +1,55 @@ +package com.baeldung.jsonparam; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.io.IOException; + +@Controller +@RestController +@RequestMapping("/api") +public class JsonParamController { + + @GetMapping(value = "/something") + public String testQueryParamApi(@RequestParam("params") 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 (JsonParseException e) { + e.printStackTrace(); + } catch (JsonMappingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return params; + } + + + @PostMapping(value = "/something") + public String testBodyParamApi(@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 (JsonParseException e) { + e.printStackTrace(); + } catch (JsonMappingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return params; + } + +} diff --git a/spring-rest-http/src/main/java/com/baeldung/jsonparam/ParamObjectDTO.java b/spring-rest-http/src/main/java/com/baeldung/jsonparam/ParamObjectDTO.java new file mode 100644 index 0000000000..a738180fa6 --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/jsonparam/ParamObjectDTO.java @@ -0,0 +1,33 @@ +package com.baeldung.jsonparam; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParamObjectDTO { + private String type; + private String color; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + @Override + public String toString() { + return "ParamObjectDTO{" + + "type='" + type + '\'' + + ", color='" + color + '\'' + + '}'; + } +}