Bethlee
Bethlee

Reputation: 945

Spring @RequestBody json 400 error, How can I change the @RequestyBody parameters?

Hi~ I have used springframework and jackson library for mapping the json data. When client send to server the json data, such as

"files": [
 {
        "fileLoctn": "%INSTALL_PATH%\\beth_birthday.jpeg",
        "fileSize": "10",
        "fileVer": "0.1"
    },
 {
        "fileLoctn": "%INSTALL_PATH%\\beth_graduate.jpeg",
        "fileSize": "10",
        "fileVer": "0.1"
    }
]}

I mapped it like this. It worked.

@RequestBody Map<String, List<InfoVO>> listInfoVo 

But When I changed the json data such as

   {
      "useId" : "Beth",
      "files": [
     {
            "fileLoctn": "%INSTALL_PATH%\\beth_birthday.jpeg",
            "fileSize": "10",
            "fileVer": "0.1"
        },
     {
            "fileLoctn": "%INSTALL_PATH%\\beth_graduate.jpeg",
            "fileSize": "10",
            "fileVer": "0.1"
        }
    ]},
    {
      "useId" : "Tom",
      "files": [
     {
            "fileLoctn": "%INSTALL_PATH%\\Tom_birthday.jpeg",
            "fileSize": "10",
            "fileVer": "0.1"
        },
     {
            "fileLoctn": "%INSTALL_PATH%\\Tom_graduate.jpeg",
            "fileSize": "10",
            "fileVer": "0.1"
        }
    ]}

I mapped @Requestbody like this.

@RequestBody List<Map<Map<String,String>, Map<String, List<InfoVO>>>> listInfoVo

But it doesn't work. How can I change the @RequestBody parameters types? I don't know. How can I change the parameters.

InfoVo contain fileLoctn, fileSize, fileVer, UserId with get/set methods.

Upvotes: 1

Views: 383

Answers (4)

chow21
chow21

Reputation: 3

You can use this site to convert json to pojo's
http://www.jsonschema2pojo.org/

Upvotes: 0

Leo
Leo

Reputation: 41

Make sure your parameter is a valid JSON format.

List<String> : ["a", "b", "c" ...]

Map<String, Object> : 
    {
        "a":{"key1":"value1","key2":"value2", ...},
        "b":{"key1":"value1","key2":"value2", ...}
    }

List<Map<String, Object>> : 
    [
        {"a":{"key1":"value1","key2":"value2", ...}}, 
        {"a":{"key1":"value1","key2":"value2", ...}}
    ]

Upvotes: 0

Bunti
Bunti

Reputation: 1760

One way of doing this could be to define a new placeholder class like this.

public class ClientRequest {
    private String userId;
    private List<InfoVO> files;
}

and the replace controller method with the following.

@RequestBody List<ClientRequest> listInfoVo

Upvotes: 1

Gervasio Amy
Gervasio Amy

Reputation: 295

I'd suggest you to define a class to map it... that data structure seems not to be so clear...

public class PayloadDTO {
  private String userId;
  private List<FileDesc> files;
  // getters and setters
}


public calss FileDescDTO {
  private String fileLoctn;
  private String fileSize;
  private String fileVer;
  // getters and setters
}

and then you can make it simple:

@RequestBody List<PayloadDTO> listInfoVo

Upvotes: 1

Related Questions