5 回答

TA貢獻1848條經驗 獲得超2個贊
您的VerificationBody課程可能如下所示:
class VerificationBody {
private String prop1;
//other properties & their getters and setter
private Map<String, ? extends Object> otherProps;
// getter setters for otherProps
}
這將使您始終能夠收到額外的屬性,而不會出現(xiàn)任何擴展問題。

TA貢獻1810條經驗 獲得超4個贊
您可以使用HashMap類似的方法來解決此類問題:
@RequestMapping(value = "/profiles/{profileId}/verify/", headers = "Accept=application/json", method = RequestMethod.POST)
public void verifyBody(@RequestBody HashMap<String, HashMap<String, String>> requestData) {
HashMap<String, String> customerInfo = requestData.get("verificationBody");
String param1 = customerInfo.get("param1");
//TODO now do whatever you want to do.
}

TA貢獻1777條經驗 獲得超10個贊
請求體的注解是@RequestBody。由于請求正文是一個鍵值對,因此將其聲明為 Map 是明智的做法。
@PostMapping("/blog")
public Blog create(@RequestBody Map<String, String> body){...}
要提取相應的鍵及其值:
String id = body.get("id");
String title = body.get("title");
String content = body.get("content");
嘗試使用此鏈接
https://medium.com/better-programming/building-a-spring-boot-rest-api-part-ii-7ff1e4384b0b

TA貢獻1998條經驗 獲得超6個贊
您可以嘗試VerificationBody像這樣修改類:
public class VerificationBody {
private String name;
private Long profileId;
// getters & setters
}
getVerificationInformation像這樣的類:
@PostMapping(value = "/profiles/{profileId}/verify/",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Response getVerificationInformation (
@RequestBody VerificationBody body) {

TA貢獻1806條經驗 獲得超8個贊
根本原因是您的 JSON 字符串無效,有效的字符串應該如下所示:
{
"name": "Example",
"profileId": "123",
"country": "US"
}
請確保每個鍵都用雙引號引起來,否則在使用Jackson.
順便說一句,我正在使用 Spring Boot,我可以通過您的代碼片段使用無效的JSON 字符串作為負載來重現(xiàn)獲取 HTTP 狀態(tài)代碼 400。
添加回答
舉報