[英]can we create a single rest API(single endpoint) which can handle different request structures (JSON) in spring boot?
I have a requirement where one rest API have to handle different request json(30+ different requests).我有一个要求,其中一个 rest API 必须处理不同的请求 json(30 多个不同的请求)。 the client can send any json to this endpoint and this API should be able to process the request.客户端可以将任何 json 发送到此端点,并且此 API 应该能够处理请求。 And every request will have unique id.每个请求都有唯一的 id。
Ex: Request 1:例如:请求 1:
{ "Id": "1", "name":"john", { “身份证”:“1”,“姓名”:“约翰”,
} }
Request 2:要求 2:
{ "Id": "2", "name":"john", "lastname":"cena", "sex":"F", { "Id": "2", "name":"john", "lastname":"cena", "sex":"F",
} }
Request 3:要求 3:
{ "Id": "3", "mobileNumber":"09XXXXXXX0", "email":"nick@yahoo.com", { "Id": "3", "mobileNumber":"09XXXXXXX0", "email":"nick@yahoo.com",
} }
Request 4:要求 4:
{ "Id": "4", "pet":"dog", "color":"black", "sex":"F", { “ID”:“4”,“宠物”:“狗”,“颜色”:“黑色”,“性别”:“F”,
} }
now, how to read requestBody for this API?现在,如何读取这个 API 的 requestBody? can we use JSONObject or JsonNode like below?我们可以像下面这样使用 JSONObject 或 JsonNode 吗?
@PostMapping("/save-details")
public String postDetails(@RequestBody JSONObject request) {
return "";
}
@PostMapping("/save-details")
public String postDetails(@RequestBody JsonNode request) {
return "";
}
Thanks for the help in Advance!我在这里先向您的帮助表示感谢!
A simple solution would be to accept the request as a json string and the use Object mapper to convert to desired type.一个简单的解决方案是将请求作为 json 字符串接受,并使用 Object 映射器转换为所需的类型。
An example with conversion and dynamically input body转换和动态输入正文的示例
package com.example.springmultirequest;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class DetailController {
Logger logger = LoggerFactory.getLogger(DetailController.class);
@PostMapping("/save-details")
public ResponseEntity<String> postDetails(@RequestBody Map<String, Object> request) {
JSONObject jsonObject = new JSONObject(request);
Integer idRequest = Integer.valueOf(jsonObject.get("Id").toString());
logger.info("idRequest: {}", idRequest);
switch (idRequest) {
case 1:
logger.info("Name: {}", jsonObject.get("name"));
break;
case 2:
logger.info("Name: {}", jsonObject.get("name"));
logger.info("Last name: {}", jsonObject.get("lastname"));
break;
case 3:
logger.info("mobileNumber: {}", jsonObject.get("mobileNumber"));
logger.info("email: {}", jsonObject.get("email"));
break;
case 4:
logger.info("pet: {}", jsonObject.get("pet"));
logger.info("color: {}", jsonObject.get("color"));
logger.info("sex: {}", jsonObject.get("sex"));
break;
default:
return ResponseEntity
.badRequest()
.body("Identification not recognized");
}
return ResponseEntity.ok().build();
}
}
Example Response:示例响应:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.3)
o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
2021-02-27 12:52:37.406 INFO 17984 --- [nio-8080-exec-1] c.e.springmultirequest.DetailController : idRequest: 1
2021-02-27 12:52:37.406 INFO 17984 --- [nio-8080-exec-1] c.e.springmultirequest.DetailController : Name: john
2021-02-27 12:52:37.622 INFO 17984 --- [nio-8080-exec-6] c.e.springmultirequest.DetailController : idRequest: 2
2021-02-27 12:52:37.622 INFO 17984 --- [nio-8080-exec-6] c.e.springmultirequest.DetailController : Name: john
2021-02-27 12:52:37.622 INFO 17984 --- [nio-8080-exec-6] c.e.springmultirequest.DetailController : Last name: cena
2021-02-27 12:52:37.676 INFO 17984 --- [nio-8080-exec-8] c.e.springmultirequest.DetailController : idRequest: 3
2021-02-27 12:52:37.676 INFO 17984 --- [nio-8080-exec-8] c.e.springmultirequest.DetailController : mobileNumber: 09XXXXXXX0
2021-02-27 12:52:37.676 INFO 17984 --- [nio-8080-exec-8] c.e.springmultirequest.DetailController : email: nick@yahoo.com
2021-02-27 12:52:37.738 INFO 17984 --- [nio-8080-exec-5] c.e.springmultirequest.DetailController : idRequest: 4
2021-02-27 12:52:37.738 INFO 17984 --- [nio-8080-exec-5] c.e.springmultirequest.DetailController : pet: dog
2021-02-27 12:52:37.738 INFO 17984 --- [nio-8080-exec-5] c.e.springmultirequest.DetailController : color: black
2021-02-27 12:52:37.738 INFO 17984 --- [nio-8080-exec-5] c.e.springmultirequest.DetailController : sex: F
Read you json with @RequestBody Map<String, Object> request, parse through your json via Json parser like Gson or Jackson. Read you json with @RequestBody Map<String, Object> request, parse through your json via Json parser like Gson or Jackson.
@PostMapping(value = "/example" )
public void abc(@RequestBody HashMap<String, String > requestData) {
System.out.println(requestData);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.