BAEL-3896: OpenAPI JSON Objects in Query Params v2
This commit is contained in:
parent
24833b7e92
commit
f0bdbde7a4
|
@ -1,19 +1,20 @@
|
||||||
package com.baeldung.jsonparam;
|
package com.baeldung.jsonparam;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
import com.baeldung.controllers.DeferredResultController;
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
public class JsonParamController {
|
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) {
|
public String testQueryParamApi(@RequestParam("params") String params) {
|
||||||
// params={"type":"foo","color":"green"}
|
// params={"type":"foo","color":"green"}
|
||||||
ParamObjectDTO paramObjectDTO;
|
ParamObjectDTO paramObjectDTO;
|
||||||
|
@ -22,18 +23,28 @@ public class JsonParamController {
|
||||||
paramObjectDTO = objectMapper.readValue(params, ParamObjectDTO.class);
|
paramObjectDTO = objectMapper.readValue(params, ParamObjectDTO.class);
|
||||||
System.out.println(paramObjectDTO);
|
System.out.println(paramObjectDTO);
|
||||||
// use paramObjectDTO where you have {"type":"foo","color":"green"} JSON data as Object
|
// use paramObjectDTO where you have {"type":"foo","color":"green"} JSON data as Object
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonProcessingException e) {
|
||||||
e.printStackTrace();
|
LOG.info("Json Processing Exception");
|
||||||
} catch (JsonMappingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return params;
|
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) {
|
public String testBodyParamApi(@RequestBody String params) {
|
||||||
// params={"type":"foo","color":"green"}
|
// params={"type":"foo","color":"green"}
|
||||||
ParamObjectDTO paramObjectDTO;
|
ParamObjectDTO paramObjectDTO;
|
||||||
|
@ -42,12 +53,8 @@ public class JsonParamController {
|
||||||
paramObjectDTO = objectMapper.readValue(params, ParamObjectDTO.class);
|
paramObjectDTO = objectMapper.readValue(params, ParamObjectDTO.class);
|
||||||
System.out.println(paramObjectDTO);
|
System.out.println(paramObjectDTO);
|
||||||
// use paramObjectDTO where you have {"type":"foo","color":"green"} JSON data as Object
|
// use paramObjectDTO where you have {"type":"foo","color":"green"} JSON data as Object
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonProcessingException e) {
|
||||||
e.printStackTrace();
|
LOG.info("Json Processing Exception");
|
||||||
} catch (JsonMappingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue