У меня есть форма, которая публикуется в моем бэкэнде (Kotlin, Spring Web). В этой форме было несколько текстовых вводов, и сообщение работало безупречно. Но когда я добавил ввод файла, пост перестал работать, возвращая следующую ошибку:
{status: 400, error: "Bad Request",…}
error: "Bad Request"
exception: "org.springframework.http.converter.HttpMessageNotReadableException"
message: "Could not read document: No suitable constructor found for type [simple type, class com.test.InsertConfigCommand]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)↵ at [Source: java.io.PushbackInputStream@2cb43211; line: 1, column: 2]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.test.InsertConfigCommand]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)↵ at [Source: java.io.PushbackInputStream@2cb43211; line: 1, column: 2]"
Вот коды моего стека:
Просмотр:
<form ng-submit="insert(config)">
<input type="text" ng-model="config.name">
<input type="text" ng-model="config.address">
<input type="file" ng-model="config.file">
<button type="submit">Save</button>
</form>
Контроллер (внешний интерфейс):
$scope.insert = function (config) {
$http.post('/config', config)
.then(function (response) {
$.snackbar({content: "Success!"});
}, $scope.showErrorMessage);
};
Контроллер (внутренняя часть):
@RequestMapping(method = arrayOf(RequestMethod.POST))
fun insert(@RequestBody config: InsertConfigCommand) = service.save(config)
InsertConfigCommand
data class InsertConfigCommand (
val name : String = "",
val address : String = "",
val file : MultipartFile
)
Я попытался сделать сообщение следующим образом, он работает, но только отправляет файл:
Контроллер (внешний интерфейс):
$scope.insert = function (file) {
var fd = new FormData();
fd.append('file', file);
return $http.post('/config', fd, {
transformRequest: angular.identity,
headers: {
'Content-Type': undefined
}
});
};
Контроллер (внутренняя часть):
@RequestMapping(method = arrayOf(RequestMethod.POST))
fun insert(@RequestParam(value = "file", required = true) file: MultipartFile) = service.save(file)
Что мне нужно изменить, чтобы этот пост работал? Я хочу отправить входной файл на тот же объект, что и имя и адрес.