我正在尝试向我的控制器发送 POST 请求,但不能传递任何类型的任何参数,除非我决定使用 JSON。我的目标是将一个字符串和一个文件传递给我的控制器,但我不断收到 Required request part 'xxx' is not present
错误。
@RestController
public class ConfigurationController {
@PostMapping(value = "/config")
public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("file") MultipartFile uploadfile){
return ResponseEntity.ok().body(null);
}
}
我不能在这里有文件。同样,如果我尝试:
@RestController
public class ConfigurationController {
@PostMapping(value = "/config")
public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("name") String name){
return ResponseEntity.ok().body(null);
}
}
同样的事情我不能在这里得到名字。
我正在通过 Postman 发送请求,如下面的屏幕截图所示:
唯一的标题标签用于授权。我没有任何 Content-Type 标头,我尝试添加 multipart/form-data
但没有帮助。
我可以传递 String 参数的唯一方法是添加到 URL。所以遵循 http://localhost:8080/SearchBox/admin/config?name=test
有效,但这不是我想要的。我想要 Body 部分中的 String 和 File 参数。
我还通过 CURL 进行了测试:
curl -X POST -H "Authorization:Bearer myToken" -H "Content-Type:Multipart/form-data" http://localhost:8080/SearchBox/admin/config --data 'pwd=pwd'
curl -X POST -H "Authorization:Bearer myToken"http://localhost:8080/SearchBox/admin/config --data 'pwd=pwd'
curl -H "Authorization:Bearer myToken" -F file=@"/g123.conf" http://localhost:8080/SearchBox/admin/config
注意: 我已经检查过类似的帖子但没有帮助 这个, 这个, 这个
原文由 Gokhan Celikkaya 发布,翻译遵循 CC BY-SA 4.0 许可协议
我终于解决了这个问题并分享了我的解决方案,以防其他人遇到同样的问题。