BAEL-3896: OpenAPI JSON Objects in Query Params - Swagger file
This commit is contained in:
parent
f0bdbde7a4
commit
5b0097f698
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue