简体   繁体   中英

Spring boot test for Restful POST API DTO with MultiPartFile attribute

I have a Spring Boot app which has a RestController with the following POST method:

@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);
    }

and UserAddDto is as follows:

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;

}

The code works fine from postman, but I have no idea how to make an integration test with MockMvc for this dto with Multipartfile object, and the test I tried gives me:

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"])

any help is appreciated.

Thanks!

I believe that you need to implements Serializable

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM