gpt4 book ai didi

java - 具有 MultiPartFile 属性的 Restful POST API DTO 的 Spring 启动测试

转载 作者:行者123 更新时间:2023-12-01 21:32:52 30 4
gpt4 key购买 nike

我有一个 Spring Boot 应用程序,它有一个带有以下 POST 方法的 RestController:

@PostMapping(path = "/add", headers = {"content-type=multipart/form-data; charset=utf-8"})
public ResponseEntity<UserWebDTO> addUser(@RequestHeader HttpHeaders headers, @ModelAttribute UserAddDTO userAddDTO) throws Exception {
return new ResponseEntity<>(userService.addUser(userAddDTO), HttpStatus.CREATED);
}

UserAddDto 如下:

public class UserAddDTO {

private String first_name;

private String last_name;

private String country_code;

private String phone_number;

private GenderEnum gender;

private String birthdate;

private MultipartFile avatar;

private String email;

}

代码在 postman 中工作正常,但我不知道如何使用 MockMvc 为这个带有 Multipartfile 对象的 dto 进行集成测试,我尝试的测试给了我:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.user.basic.authentication.dtos.UserAddDTO["avatar"]->org.springframework.mock.web.MockMultipartFile["inputStream"])

感谢任何帮助。

谢谢!

最佳答案

我相信你需要实现 Serialized

public class UserAddDTO implements Serializable {
private static final long serialVersionUID = 1L;

private String first_name;

private String last_name;

private String country_code;

private String phone_number;

private GenderEnum gender;

private String birthdate;

private MultipartFile avatar;

private String email;

}

关于java - 具有 MultiPartFile 属性的 Restful POST API DTO 的 Spring 启动测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58810237/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com