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 deleted file mode 100644 index d2a09296c2..0000000000 --- a/spring-rest-http/src/main/java/com/baeldung/jsonparam/JsonParamController.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.baeldung.jsonparam; - -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.*; - -@Controller -@RestController -@RequestMapping("/api") -public class JsonParamController { - private final static Logger LOG = LoggerFactory.getLogger(DeferredResultController.class); - - @GetMapping(value = "/tickets") - 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 (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 = "/tickets") - 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 (JsonProcessingException e) { - LOG.info("Json Processing Exception"); - } - 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 deleted file mode 100644 index a738180fa6..0000000000 --- a/spring-rest-http/src/main/java/com/baeldung/jsonparam/ParamObjectDTO.java +++ /dev/null @@ -1,33 +0,0 @@ -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 + '\'' + - '}'; - } -} diff --git a/spring-rest-http/src/main/java/com/baeldung/jsonparam/jsonParamOpenApiSwaggerDefinition b/spring-rest-http/src/main/java/com/baeldung/jsonparam/jsonParamOpenApiSwaggerDefinition new file mode 100644 index 0000000000..9f9bcb788a --- /dev/null +++ b/spring-rest-http/src/main/java/com/baeldung/jsonparam/jsonParamOpenApiSwaggerDefinition @@ -0,0 +1,154 @@ +swagger: '2.0' +... +paths: + /tickets: + get: + parameters: + - in: query + name: params + required: true + description: A JSON object with the `type` and `color` properties + type: string + example: '{"type":"foo","color":"green"}' + +swagger: '2.0' +... +paths: + /tickets: + post: + requestBody: + description: Parameter is an object that should be serialized as JSON + content: + application/json: + schema: + type: string + example: '{"type":"foo","color":"green"}' + responses: + '200': + description: successful process + +"/api/tickets": { + "get": { + "tags": [ + "account-resource" + ], + "summary": "testQueryParamApi", + "operationId": "testQueryParamApiUsingGET", + "produces": [ + "*/*" + ], + "parameters": [ + { + "name": "params", + "in": "query", + "description": "params", + "required": true, + "type": "string" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + }, + "401": { + "description": "Unauthorized" + }, + "403": { + "description": "Forbidden" + }, + "404": { + "description": "Not Found" + } + }, + "deprecated": false + }, + "post": { + "tags": [ + "account-resource" + ], + "summary": "testBodyParamApi", + "operationId": "testBodyParamApiUsingPOST", + "consumes": [ + "application/json" + ], + "produces": [ + "*/*" + ], + "parameters": [ + { + "in": "body", + "name": "params", + "description": "params", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + }, + "201": { + "description": "Created" + }, + "401": { + "description": "Unauthorized" + }, + "403": { + "description": "Forbidden" + }, + "404": { + "description": "Not Found" + } + }, + "deprecated": false + } +}, +"/api/tickets2": { + "get": { + "tags": [ + "account-resource" + ], + "summary": "testGetBodyParamApi", + "operationId": "testGetBodyParamApiUsingGET", + "produces": [ + "*/*" + ], + "parameters": [ + { + "in": "body", + "name": "params", + "description": "params", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + }, + "401": { + "description": "Unauthorized" + }, + "403": { + "description": "Forbidden" + }, + "404": { + "description": "Not Found" + } + }, + "deprecated": false + } +}