-
form-data RequestDTO 매핑 @Setter 필수BackEnd/스프링 2022. 7. 23. 23:45
Postman 으로 보내는 form-data 가 BoardRequest 에 mapping 이 잘되지 않고 에러가 나거나 null 값이 들어왔다.
결론적으로는 BoardRequest 에 @Setter 를 추가했더니 값이 잘 들어오는 것을 확인했다.
@PostMapping(value="/boards" , consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE} ) public ResponseEntity<Long> save( @Valid BoardRequest request ){ ... }
RequestDTO
public class BoardRequest { private Title title; private Content content; private Status status; private Long memberId; private List<MultipartFile> files; .. }
postman
1. 처음에는 ObjectMapper 는 기본생성자만 있으면 된다고 생각해서 기본생성자와 Setter 만 두었다.
하지만 ObejctMapper 는 Json 매핑에 사용되지, 여기서는 Form-data 기 때문에 값이 세팅되지 않는 것 같다.
하지만 생성자를 통해 작업을 하는지 access Level 을 다르게 둘 때마다 default value must not be null 같은 에러가 발생했다.
2. List<MultipartFile> 을 postman 에서 보낼 때 배열형태로 보내야한다는 포스팅을 보고
files[0].file 으로 시도했는데 잘되지 않았다.
@NoArgContructor
@AllArgConstructor
postman RequestDTO 결과 시도1 files List files = new ArrayList() 에러 없음
값 null시도2 files List files; default value must not be null 시도3 files[0].file List files; default value must not be null 시도4 files[0].file List files = new ArrayList() "Invalid property 'files[0]' of bean class [com.my.blog.board.dto.request.BoardRequest]
...
NullValueInNestedPathException
NoSuchMethodException시도5 files.file List files = new ArrayList() 에러 없음
값 null@Setter 추가
시도6 files List files ; 에러 없음
값 정상List 를 postman 에서 배열형식으로 보내야한다는 포스팅
https://galid1.tistory.com/754
Spring Boot - 개발자를 기억하게하지 말자 (파일리스트와 데이터리스트 요청을 하나의 객체로 바인
file과 data를 객체로 바인딩하기 Spring을 이용해 Rest API 개발중, file과 data를 같이 업로드해야 하는 상황이 있었습니다. file과 data는 논리적으로 연관관계가 있었으
galid1.tistory.com
MultiPart 용 custom jackson deserializer 를 생성해야한다는 답변
Spring Controller - Testing to receive nested dto with multipartfile inside with postman
I'm working on a service which accepts a DTO in a POST method and creates an entity based on that DTO. Nested inside is a multipart file, which is going to be an image used by the entity that will be
stackoverflow.com
반응형'BackEnd > 스프링' 카테고리의 다른 글
@Builder 위치와 연관관계 세팅 (0) 2022.07.24 @Value 는 객체 bean 등록 필수, static 키워드 사용 x (0) 2022.07.23 Test > BDD (0) 2022.06.28 스프링 시큐리티 정리 (0) 2022.06.24 스프링 테스트 MOCK (0) 2022.06.03