我在为控制器运行 junit 时遇到以下错误。我已经将 content-type 设置为 Json,但错误仍然相同。有什么建议可能是什么问题吗?
错误是
java.lang.AssertionError: expectation "expectNext({response=employee saved Successfully})" failed (expected: onNext({response=employee saved Successfully}); actual: onError(org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/octet-stream' not supported for bodyType=java.util.Map<java.lang.String, java.lang.String>))
Run Code Online (Sandbox Code Playgroud)
控制器是
@Slf4j
@Controller
@RequiredArgsConstructor
public class EmployeeController {
private final EmployeeService employeeService;
@PostMapping(path ="/employees",produces = APPLICATION_JSON_VALUE)
public @ResponseBody Mono<Map<String, String>> saveEmployees(@RequestBody List<EmployeeDto> employeeDtos) {
log.info("Received request to save employees [{}]", employeeDtos);
return employeeService.saveEmployee(employeeDtos);
}
}
Run Code Online (Sandbox Code Playgroud)
服务等级如下:
@Service
@Slf4j
@RequiredArgsConstructor
public class EmployeeService {
private final WebClient webClient;
public Mono<Map<String, String>> saveEmployees(List<EmployeeDto> employeeDtos) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过我的 React 前端向 Spring 后端发送 API 请求。当我发送请求时,我收到此错误:
Could not resolve parameter [0] in private org.springframework.http.ResponseEntity com.example.bottlecap.controllers.BottlecapController.entryForm(com.example.bottlecap.domian.Bottlecap,org.springframework.web.multipart.MultipartFile): Content type 'application/octet-stream' not supported
Run Code Online (Sandbox Code Playgroud)
但是,当我使用 Postman 设置请求时,它运行良好。我认为我在 React 端设置 FormData 的方式可能存在问题。但是,我几乎没有运气弄清楚。
我的 API 应该接收一个对象,该对象包含有关我提交的数据以及与提交相关的图像。在我的 Postman 请求中,我创建了一个表单数据,其中包含一个 JSON 文件,该文件包含所有对象数据和一个仅用于测试的随机图像。正如我所说,requets 通过这个很好。但是,在前端代码中,我将对象数据解析为 Json 并将其添加到 FormData 以及将图像添加到 FormData。
这是我的弹簧控制器:
@RequestMapping(path ="/bottlecap/entry", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE})
private ResponseEntity entryForm(@RequestPart("cap") Bottlecap cap, @RequestPart("file") MultipartFile image){
System.out.println(cap);
cap.toString();
System.out.println("Test");
return ResponseEntity.ok().build();
}
Run Code Online (Sandbox Code Playgroud)
这是我的反应前端表单提交处理程序:
handleSubmit = event =>{
console.log(this.state.page);
console.log(this.state.page);
event.preventDefault();
const cap ={
"name":this.state.name,
"brand":this.state.brand,
"yearMade":parseInt(this.state.yearMade),
"country": …Run Code Online (Sandbox Code Playgroud)