SW개발/Spring Framework

HttpHeaders 추가시 UnsupportedOperationException

개소왕 2020. 2. 12. 16:11

* 개요

ResponseEntity#getHeaders().setContentType(...) 를 호출했는데 

UnsupportedOperationException 발생

 

* 원인

getHeaders() 로 HttpHeaders 가져올때는 Collections.unmodifiableList() 를 씌워서 반환함.

헤더 설정은 생성당시에만 하고, 그 뒤에는 보기만 하라는거.

 

* 해결

헤더는 생성시에 설정함.

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Content-Type", "application/json; charset=UTF-8");
ResponseEntity response = new ResponseEntity(/*바디*/, headers, HttpStatus.OK);