邮递员表单数据发送带有文件的复杂对象

时间:2018-08-10 08:31:31

标签: java spring file-upload postman

如何使用Postman中的文件发送(或可能无法发送)复杂对象

我的对象:

class Client {
    private String clientName;
    private Platform platform;
}

class Platform {
    private String android;
    private String ios;
}

我的控制器类:

@PostMapping(value = "/evaluate", produces = "application/json")
public ResponseEntity<ServerResponse> sendEvaluateForm(Client client,
        @RequestParam(value = "files", required = false) MultipartFile files)
{
    return new ResponseEntity<>(HttpStatus.OK);
}

这就是我在邮递员中发送请求的方式: enter image description here

当我传递“ clientName”(客户端的基本字段)时,它起作用。 但是我不知道如何传递平台对象。 我试图传递密钥:平台 值:{“ android”:“ asd”,“ ios”:“ xxx”} 但是我只有BadRequest(400)

2 个答案:

答案 0 :(得分:3)

使用邮递员,您可以同时建立一个包含文件对象的请求。

结果应作为后端请求正文

{ street: '69 Pinapple street', city: 'Apple', zip: 6969, country: 'Pen' }

Postman ScreenShoot

答案 1 :(得分:1)

您可以尝试将客户数据作为纯字符串发送并在控制器端进行解析。

    @PostMapping(value = "/evaluate", produces = "application/json")
    public ResponseEntity<?> sendEvaluateForm(@RequestParam ("client") String client,
                                                               @RequestParam(value = "files", required = false) MultipartFile files) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        Client clientobject = mapper.readValue(client, Client.class);

        return ResponseEntity.ok().build();
    }

邮递员要求:

enter image description here

以及您的POJO类:

class Client {
    private String clientName;
    private Platform platform;

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public Platform getPlatform() {
        return platform;
    }

    public void setPlatform(Platform platform) {
        this.platform = platform;
    }
}

class Platform {
    private String android;
    private String ios;

    public String getAndroid() {
        return android;
    }

    public void setAndroid(String android) {
        this.android = android;
    }

    public String getIos() {
        return ios;
    }

    public void setIos(String ios) {
        this.ios = ios;
    }
}