第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

訪問 Spring @RequestBody 中動態(tài)添加的屬性

訪問 Spring @RequestBody 中動態(tài)添加的屬性

慕的地6264312 2023-12-10 15:11:31
我有一個如下所示的帖子映射:@PostMapping(value = "/profiles/{profileId}/verify/")public Response getVerificationInformation (    @RequestBody VerificationBody body) {    ... do something with the body    ... call function A with body object}隨后在函數(shù) A 中,我訪問 body 對象的一些屬性。另外,在前端,我正在修改命中此后映射的 JSON 對象(我正在添加另一個屬性)。例如,{    "name" : "Example",    "profileId" : "123",    // and I am dynamically adding an attribute 'country'    "country" : "US"}問題出在函數(shù) A 處,我無法獲取有關動態(tài)添加的屬性(在本例中為“國家/地區(qū)”)的信息。為動態(tài)添加的屬性聲明 getter 并不理想,因為動態(tài)添加的屬性很多。我已經嘗試了 @JsonAnySetter 和 @JsonAnyGetter 的方向,但我得到了 400。我正在尋找其他解決方案。請幫忙并提前致謝?。ㄎ液喕艘恍┳兞亢秃瘮?shù)的名稱,但我希望它不會太難理解)。VerificationBody 可以被認為如下:public class VerificationBody {    @JsonProperty(value = "name")    String name,    @JsonProperty(value = "profileId")    Long profileId,    // ... it does not include country}設法解決 400 問題,我可以通過 JsonAnyGetter 和 JsonAnySetter 獲取屬性。
查看完整描述

5 回答

?
慕尼黑5688855

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)任何擴展問題。


查看完整回答
反對 回復 2023-12-10
?
蝴蝶不菲

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.

}


查看完整回答
反對 回復 2023-12-10
?
不負相思意

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


查看完整回答
反對 回復 2023-12-10
?
米琪卡哇伊

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) {


查看完整回答
反對 回復 2023-12-10
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

根本原因是您的 JSON 字符串無效,有效的字符串應該如下所示:


{

  "name": "Example",

  "profileId": "123",

  "country": "US"

}

請確保每個鍵都用雙引號引起來,否則在使用Jackson.


順便說一句,我正在使用 Spring Boot,我可以通過您的代碼片段使用無效的JSON 字符串作為負載來重現(xiàn)獲取 HTTP 狀態(tài)代碼 400。


查看完整回答
反對 回復 2023-12-10
  • 5 回答
  • 0 關注
  • 280 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號