BAEL-3896: OpenAPI JSON Objects in Query Params v2

This commit is contained in:
alex.peptan 2020-04-21 19:05:42 +03:00
parent 24833b7e92
commit f0bdbde7a4
1 changed files with 25 additions and 18 deletions

View File

@ -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;
}