Java 编译角度代码时无法上载文件
我正在尝试将文件上载到此BE Spring代码:Java 编译角度代码时无法上载文件,java,angular,spring,typescript,spring-boot,Java,Angular,Spring,Typescript,Spring Boot,我正在尝试将文件上载到此BE Spring代码: @PostMapping(value = "/upload", produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<StringResponseDTO> uploadFile(@RequestParam("file") MultipartFile file, RedirectAttributes redirectA
@PostMapping(value = "/upload", produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<StringResponseDTO> uploadFile(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes, @RequestParam("id") Integer merchantId) throws Exception {
..............
return ResponseEntity.ok(new StringResponseDTO("test"));
}
当我在本地运行代码时,它工作正常。我得到http代码200和response{“response”:{}}
。但是当我编译代码并在Apache上运行它时,我得到一个错误:
SyntaxError:JSON中位于位置0的意外标记<
完全错误:
调试后,我将其放入响应内容:
你能给我一些建议如何解决这个问题吗
是否有其他解决方案来实现文件上传?可能我只需要返回状态OK?
SyntaxError:Unexpected token<在JSON中的位置0
表示您的服务器(可能)返回HTML(或XML)文件而不是JSON。打开开发工具来检查响应。是的-因此,您要么击中了错误的端点,要么服务器错误地返回了index.html
。不要只看devtools中的响应,URL是什么?
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonTypeName("response")
public class StringResponseDTO {
private Map<String, String> errors;
public StringResponseDTO(String redirect) {
super();
}
public StringResponseDTO(Map<String, String> errors) {
this.errors = errors;
}
public Map<String, String> getErrors() {
return errors;
}
public void setErrors(Map<String, String> errors) {
this.errors = errors;
}
}
imports(file: any, id: number) {
const formData = new FormData();
formData.append('file', file, file.name);
return this.http.post(environment.api.urls.merchants.uploadLogo, formData, {
params: { id: id.toString() }
}).pipe(
catchError(this.handleError)
);
}