Spring Boot 2.7 + Mustache 사용 시 한글이 깨지는 문제

코딩하는 감자·2023년 4월 3일
0

Spring Boot 2.7.9와 Mustache를 사용하던 도중 위와 같이 한글이 전부 깨지는 문제가 발생했다.
그래서 응답의 헤더를 확인해보니,


Content-type 헤더의 charset이 UTF-8이 아닌 ISO-8859-1로 되어있었다.
ISO-8859-1은 HTTP1.1의 기본 charset이고, 한글을 지원하지 않는다.

해결 방법

방법 1. properties에 인코딩 설정 추가

application.properties 에

server.servlet.encoding.force=true

를 추가한다.
위 코드는 SpringBoot 공식 문서에 따르면 HTTP 요청과 응답의 인코딩을 내가 지정한 charset으로 설정한다.

참고 링크

https://stackoverflow.com/questions/48266913/springboot-response-charset-errors
https://velog.io/@d-h-k/SpringBoot-%ED%95%9C%EA%B8%80-%EC%B6%9C%EB%A0%A5..-%EB%8F%84%EC%A0%80%ED%9E%88-%EC%95%88%EB%90%A0%EB%95%8C
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#application-properties.server.server.servlet.encoding.force

방법 2. Spring Boot 버전 내리기

build.gradle에서

id 'org.springframework.boot' version '2.6.8'

로 Spring Boot 버전을 내린다. (꼭 2.6.8이 아니어도 되고, 2.7.0 아래이기만 하면 된다.)
하지만 버전을 내리면 상위 버전의 기능을 사용하지 못하게 되므로 웬만하면 다른 방법으로 해결하는 게 좋을 것 같다.
인코딩 때문에 프레임워크의 버전까지 내릴 건 아닌 것 같다...

SpringBoot 2.7 릴리즈 노트를 보면,

Servlet-specific Mustache Properties

The following Mustache-related properties that are Servlet-specific have been deprecated:

spring.mustache.allow-request-override
spring.mustache.allow-session-override
spring.mustache.cache
spring.mustache.content-type
spring.mustache.expose-request-attributes
spring.mustache.expose-session-attributes
spring.mustache.expose-spring-macro-helpers

The following replacements have been introduced:

spring.mustache.servlet.allow-request-override
spring.mustache.servlet.allow-session-override
spring.mustache.servlet.cache
spring.mustache.servlet.content-type
spring.mustache.servlet.expose-request-attributes
spring.mustache.servlet.expose-session-attributes
spring.mustache.servlet.expose-spring-macro-helpers

Mustache properties 관련 변경 사항이 있었음을 알 수 있다.
(자세한 건 모르겠지만 Spring Boot 2.6에서는 한글 깨짐 문제가 발생하지 않으니 아마 관련이 있을 듯 하다.)

참고 링크

https://velog.io/@jihye/mustache%EC%97%90%EC%84%9C-%ED%95%9C%EA%B8%80%EC%9D%B4-%EA%B9%A8%EC%A7%84%EB%8B%A4-%EB%9A%AB%EB%9A%8F

번외

찾아보니 Spring Boot 레포지토리에도 이 문제에 대한 이슈가 있었는데, 컨트리뷰터님의 환경에서는 한글이 안깨지고 잘 돌아가서 컨트리뷰터님도 해결을 못하신 것 같다...

0개의 댓글