代码之家  ›  专栏  ›  技术社区  ›  xetra11

在Spring MVC中不能将有效的JSON数组作为请求主体

  •  0
  • xetra11  · 技术社区  · 6 年前

    考虑下列JSON有效载荷,其有效性是通过 https://jsonformatter.curiousconcept.com/

    [  
       {  
          "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());
      }
    

    哪里 Characters

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

    Character

    @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/"
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   GauravRai1512    6 年前

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

    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
           }
        ]
    }
    

    @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());
          }
    
        2
  •  2
  •   luk2302    6 年前

    JSON与您的 Characters 班级。JSON应该是:

    {
        characters: [ { "uid" : "6558204851", ... }, { ... } ]
    }