양식이 백 엔드에 게시됩니다 (Kotlin, Spring Web). 이 양식에는 일부 텍스트 입력이 있었으며 게시물은 완벽하게 작동했습니다.각도 : 양식 제출시 서버에 파일을 게시하는 방법
보기 :
<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>
컨트롤러 여기
{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: [email protected]; 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: [email protected]; line: 1, column: 2]"
내 스택의 코드입니다 : 내가 파일 입력을 추가하지만, 포스트는 다음과 같은 오류를 반환 작동이 중지 프런트 엔드 :
$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)
어떤이 게시물 작동하려면 나는 변경해야합니까? 입력 파일 같은 개체에 그 이름과 주소를 보내고 싶습니다.
죄송합니다. 작동하지 않았습니다. 필자는 그것이 파일 매개 변수없이 게시물을 처리 할 때 구문 분석이 작동하기 때문에 잭슨 문제라고 생각하지 않습니다. –