gpt4 book ai didi

java - Spring MVC 中无法将有效的 JSON 数组作为请求正文

转载 作者:行者123 更新时间:2023-12-01 19:46:11 25 4
gpt4 key购买 nike

考虑以下通过 https://jsonformatter.curiousconcept.com/ 有效的 JSON 负载

[  
{
"uid":"6558204851",
"slot":2,
"name":"Denis MacIntyre",
"role":"coopr_role_dmr",
"state":"coopr_state_ok",
"position":"[12292.1,8900.03,0.00141907]",
"timestampWIA":12306.9,
"loadout":"[['BWA3_MG3','','','',['BWA3_120Rnd_762x51_soft',120],[],'BWA3_bipod_MG3'],[],['BWA3_P8','','','',['BWA3_15Rnd_9x19_P8',15],[],''],['BWA3_Uniform2_sleeves_Fleck',[['ACE_key_west',1],['ACRE_SEM52SL_ID_1',1],['BWA3_DM51A1',1,1],['BWA3_15Rnd_9x19_P8',4,15]]],['BWA3_Vest_Grenadier_Fleck',[['ACE_fieldDressing',1],['ACE_elasticBandage',8],['ACE_quikclot',1],['ACE_EarPlugs',1],['ACE_epinephrine',1],['ACE_morphine',2],['ACE_personalAidKit',1],['BWA3_DM51A1',2,1],['1Rnd_Smoke_Grenade_shell',3,1],['BWA3_120Rnd_762x51_soft',1,120],['BWA3_120Rnd_762x51_Tracer_soft',1,120]]],['BWA3_PatrolPack_Fleck',[['ACE_MapTools',1],['ACE_tourniquet',2],['ACE_Flashlight_MX991',1],['ACE_morphine',2],['ACE_SpraypaintBlue',1],['BWA3_DM32_Red',1,1],['BWA3_DM32_Green',1,1],['BWA3_DM25',5,1],['BWA3_DM32_Orange',1,1],['Chemlight_blue',2,1],['Chemlight_green',2,1],['BWA3_DM51A1',1,1],['BWA3_120Rnd_762x51_Tracer_soft',6,120]]],'BWA3_M92_Fleck','BWA3_G_Combat_clear',['Binocular','','','',[],[],''],['ItemMap','BWA3_ItemNaviPad','ItemRadioAcreFlagged','ItemCompass','ItemWatch','CUP_NVG_HMNVS']]",
"reputation":10,
"legacy":3
},
{
"uid":"6845626561",
"slot":1,
"name":"Antoinette Klimuk",
"role":"coopr_role_engineer",
"state":"coopr_state_ok",
"position":"[12292.1,8900.03,0.00141907]",
"timestampWIA":12306.9,
"loadout":"[['BWA3_G36A1','','BWA3_acc_LLM01_irlaser','',['BWA3_30Rnd_556x45_G36',30],[],''],[],['BWA3_P8','','','',['BWA3_15Rnd_9x19_P8',15],[],''],['BWA3_Uniform2_sleeves_Fleck',[['ACE_key_west',1],['ACRE_SEM52SL_ID_1',1],['BWA3_DM51A1',1,1],['BWA3_15Rnd_9x19_P8',1,15],['BWA3_DM25',1,1],['BWA3_30Rnd_556x45_G36',1,30]]],['BWA3_Vest_Grenadier_Fleck',[['ACE_fieldDressing',1],['ACE_elasticBandage',8],['ACE_quikclot',1],['ACE_EarPlugs',1],['ACE_epinephrine',1],['ACE_morphine',2],['ACE_DefusalKit',1],['ACE_EntrenchingTool',1],['BWA3_DM51A1',2,1],['1Rnd_Smoke_Grenade_shell',3,1],['BWA3_30Rnd_556x45_G36',7,30],['DemoCharge_Remote_Mag',1,1],['APERSMine_Range_Mag',1,1]]],['BWA3_PatrolPack_Fleck',[['ACE_MapTools',1],['ACE_tourniquet',2],['ACE_Flashlight_MX991',1],['ACE_morphine',2],['ACE_SpraypaintBlue',1],['ACE_wirecutter',1],['ACE_Clacker',1],['ToolKit',1],['BWA3_DM32_Red',1,1],['BWA3_DM32_Green',1,1],['BWA3_DM25',5,1],['BWA3_DM32_Orange',1,1],['Chemlight_blue',2,1],['Chemlight_green',2,1],['BWA3_DM51A1',1,1],['DemoCharge_Remote_Mag',1,1],['BWA3_DM31AT_Mag',1,1],['APERSMine_Range_Mag',2,1],['APERSTripMine_Wire_Mag',1,1],['BWA3_30Rnd_556x45_G36',1,30]]],'BWA3_M92_Fleck','BWA3_G_Combat_clear',['Binocular','','','',[],[],''],['ItemMap','BWA3_ItemNaviPad','ItemRadioAcreFlagged','ItemCompass','ItemWatch','CUP_NVG_HMNVS']]",
"reputation":25,
"legacy":6
}
]

当针对以下 POST 端点使用它时

  @PostMapping(value = "/save/characters/", consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateCharacters(@RequestBody Characters characters) {
mongoTemplate.save(characters);
log.log(Level.INFO, "{} characters have been saved", characters.getCharacters().size());
}

字符在哪里

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Characters {
private List<Character> characters;
}

字符

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "my_collection")
public class Character {
// Meta
@Id
private String uid;
private int slot;
private String name;
private String role;
// State
private String state;
private String position;
private double timestampWIA;
// Equipment
private String loadout;
// Scores
private int reputation;
private int legacy;

}

失败并显示以下内容

POST http://localhost:8080/save/characters/

HTTP/1.1 400
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 13 Nov 2018 16:38:27 GMT
Connection: close

{
"timestamp": "2018-11-13T16:38:27.161+0000",
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot deserialize instance of `com.coopr.hq.models.Characters` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.coopr.hq.models.Characters` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]"
"path": "/save/characters/"
}

最佳答案

您的 json 格式不正确,它应该以如下字符开头:正如@luk2302提到的

您的 pojo 类映射应该与您的 json 类似,在 json 中您没有字符变量,但在 pojo 中您正在创建一个具有此名称的变量,或者从 pojo 中删除此变量或在 json 中添加此变量。

{
characters:
[
{
"uid":"6558204851",
"slot":2,
"name":"Denis MacIntyre",
"role":"coopr_role_dmr",
"state":"coopr_state_ok",
"position":"[12292.1,8900.03,0.00141907]",
"timestampWIA":12306.9,
"loadout":"[['BWA3_MG3','','','',['BWA3_120Rnd_762x51_soft',120],[],'BWA3_bipod_MG3'],[],['BWA3_P8','','','',['BWA3_15Rnd_9x19_P8',15],[],''],['BWA3_Uniform2_sleeves_Fleck',[['ACE_key_west',1],['ACRE_SEM52SL_ID_1',1],['BWA3_DM51A1',1,1],['BWA3_15Rnd_9x19_P8',4,15]]],['BWA3_Vest_Grenadier_Fleck',[['ACE_fieldDressing',1],['ACE_elasticBandage',8],['ACE_quikclot',1],['ACE_EarPlugs',1],['ACE_epinephrine',1],['ACE_morphine',2],['ACE_personalAidKit',1],['BWA3_DM51A1',2,1],['1Rnd_Smoke_Grenade_shell',3,1],['BWA3_120Rnd_762x51_soft',1,120],['BWA3_120Rnd_762x51_Tracer_soft',1,120]]],['BWA3_PatrolPack_Fleck',[['ACE_MapTools',1],['ACE_tourniquet',2],['ACE_Flashlight_MX991',1],['ACE_morphine',2],['ACE_SpraypaintBlue',1],['BWA3_DM32_Red',1,1],['BWA3_DM32_Green',1,1],['BWA3_DM25',5,1],['BWA3_DM32_Orange',1,1],['Chemlight_blue',2,1],['Chemlight_green',2,1],['BWA3_DM51A1',1,1],['BWA3_120Rnd_762x51_Tracer_soft',6,120]]],'BWA3_M92_Fleck','BWA3_G_Combat_clear',['Binocular','','','',[],[],''],['ItemMap','BWA3_ItemNaviPad','ItemRadioAcreFlagged','ItemCompass','ItemWatch','CUP_NVG_HMNVS']]",
"reputation":10,
"legacy":3
},
{
"uid":"6845626561",
"slot":1,
"name":"Antoinette Klimuk",
"role":"coopr_role_engineer",
"state":"coopr_state_ok",
"position":"[12292.1,8900.03,0.00141907]",
"timestampWIA":12306.9,
"loadout":"[['BWA3_G36A1','','BWA3_acc_LLM01_irlaser','',['BWA3_30Rnd_556x45_G36',30],[],''],[],['BWA3_P8','','','',['BWA3_15Rnd_9x19_P8',15],[],''],['BWA3_Uniform2_sleeves_Fleck',[['ACE_key_west',1],['ACRE_SEM52SL_ID_1',1],['BWA3_DM51A1',1,1],['BWA3_15Rnd_9x19_P8',1,15],['BWA3_DM25',1,1],['BWA3_30Rnd_556x45_G36',1,30]]],['BWA3_Vest_Grenadier_Fleck',[['ACE_fieldDressing',1],['ACE_elasticBandage',8],['ACE_quikclot',1],['ACE_EarPlugs',1],['ACE_epinephrine',1],['ACE_morphine',2],['ACE_DefusalKit',1],['ACE_EntrenchingTool',1],['BWA3_DM51A1',2,1],['1Rnd_Smoke_Grenade_shell',3,1],['BWA3_30Rnd_556x45_G36',7,30],['DemoCharge_Remote_Mag',1,1],['APERSMine_Range_Mag',1,1]]],['BWA3_PatrolPack_Fleck',[['ACE_MapTools',1],['ACE_tourniquet',2],['ACE_Flashlight_MX991',1],['ACE_morphine',2],['ACE_SpraypaintBlue',1],['ACE_wirecutter',1],['ACE_Clacker',1],['ToolKit',1],['BWA3_DM32_Red',1,1],['BWA3_DM32_Green',1,1],['BWA3_DM25',5,1],['BWA3_DM32_Orange',1,1],['Chemlight_blue',2,1],['Chemlight_green',2,1],['BWA3_DM51A1',1,1],['DemoCharge_Remote_Mag',1,1],['BWA3_DM31AT_Mag',1,1],['APERSMine_Range_Mag',2,1],['APERSTripMine_Wire_Mag',1,1],['BWA3_30Rnd_556x45_G36',1,30]]],'BWA3_M92_Fleck','BWA3_G_Combat_clear',['Binocular','','','',[],[],''],['ItemMap','BWA3_ItemNaviPad','ItemRadioAcreFlagged','ItemCompass','ItemWatch','CUP_NVG_HMNVS']]",
"reputation":25,
"legacy":6
}
]
}

注意:您还应该在调用后使用 Produce,如下所示:

@PostMapping(value = "/save/characters/", produces=MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateCharacters(@RequestBody Characters characters) {
mongoTemplate.save(characters);
log.log(Level.INFO, "{} characters have been saved", characters.getCharacters().size());
}

还有另一种方法,不要更改您的 json,只需删除 Characters 类并保留 Character 类并更改您的代码,如下所示:

@PostMapping(value = "/save/characters/", produces=MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateCharacters(@RequestBody List<Character> character) {
mongoTemplate.save(characters);
log.log(Level.INFO, "{} characters have been saved", characters.getCharacters().size());
}

关于java - Spring MVC 中无法将有效的 JSON 数组作为请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53285684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com