BAEL-3896: OpenAPI JSON Objects in Query Params
This commit is contained in:
parent
a057b3af44
commit
24833b7e92
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue