Mol*_*ler 6 java spring file-upload postman
如何在 Postman 中发送(或者可能不可能?)带有文件的复杂对象
我的对象:
class Client {
private String clientName;
private Platform platform;
}
class Platform {
private String android;
private String ios;
}
Run Code Online (Sandbox Code Playgroud)
我的控制器类:
@PostMapping(value = "/evaluate", produces = "application/json")
public ResponseEntity<ServerResponse> sendEvaluateForm(Client client,
@RequestParam(value = "files", required = false) MultipartFile files)
{
return new ResponseEntity<>(HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
当我传递客户端中的基本字段“clientName”时,它就起作用了。但我不知道如何传递 Platform 对象。我试图传入 key: platform 和 value: {"android" : "asd", "ios" : "xxx"} 但我只得到了 BadRequest(400)
小智 12
使用Postman,您可以构建同时包含文件和对象的请求。
结果预期为后端req.body:
{ street: '69 Pinapple street', city: 'Apple', zip: 6969, country: 'Pen' }
您可以尝试将您的客户端数据作为纯字符串发送并在控制器端解析它。
@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();
}
Run Code Online (Sandbox Code Playgroud)
邮递员要求:
还有你的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12320 次 |
| 最近记录: |