SpringBoot 2 http response utf-8 μ€μ νκΈ°
μ€νλ§λΆνΈ 1 μ μ¬μ©νλ μν°νλΌμ΄μ¦ μ±μ MSA μ μ© μμ μ ν΅ν΄ μ¬λ¬ μ±μΌλ‘ λΆλ¦¬νλ©΄μ μ€νλ§λΆνΈ 2λ‘ μ κ·Έλ μ΄λλ₯Ό μ§ννμ΅λλ€.
μ΄ κ³Όμ μμ λ€λ₯Έ νμ¬μ API μ°λνκ³ μλ λΆλΆμμ μΈμ½λ© λ¬Έμ κ° λ°μνμ΅λλ€.
μμ€ μ΄μ μ μ§ννλ©΄μ λΉΌλ¨Ήμ λΆλΆμ΄ μμ΄μμμ£ .
μ΄μκ° λ°μν νμ¬μΈ‘μμ μμΈμΌλ‘ μκΈ°ν κ²μ μλ΅ ν€λμ Content-Typeμ utf-8 μΈμ½λ© μ€μ μ΄ λΉ μ Έμλ€λ κ²μ΄μμ΅λλ€.
κ·Έλμ νμΈν΄λ³΄λ μ€νλ§λΆνΈ 1 κΈ°λ° μ±μμλ μλμ κ°μ μ€μ μ΄ μμμ΅λλ€.
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
return new StringHttpMessageConverter(Charset.forName("UTF-8"));
}
@Bean
public Filter characterEncodingFilter() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return characterEncodingFilter;
}
κ·Έλ¦¬κ³ ν μ€νΈ κ²°κ³Ό μλ΅ν€λμ Content-Typeμ application/json;charset=utf-8 μ΄λΌκ³ λμ΄μλ κ²μ νμΈν μ μμμ΅λλ€.
νμ§λ§ μ κ· μ±μμλ charset μ λ³΄κ° λΉ μ Έμμμ΅λλ€.
κ·Έλμ μ μ€μ μ κ·Έλλ‘ μ€νλ§λΆνΈ 2 κΈ°λ°μ μ κ·λ‘ κ°λ°ν μ±μͺ½μ λ£μΌλ € νλλ μ± κΈ°λ μ μ€λ₯κ° λ°μνμ΅λλ€.
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'characterEncodingFilter', defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfiguration.class], could not be registered. A bean with that name has already been defined in com.yanolja.affiliate.out.api.AffiliateOutApiApplication and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
μ΄λ―Έ λμΌν μ΄λ¦μ λΉμ΄ μ‘΄μ¬νλ μ€λ²λΌμ΄λ ν μ μλλ‘ μ€μ μ λ³κ²½νλΌλ μκΈ°μμ£ .
κ·Όλ° μκ°ν΄λ³΄λ κ΅³μ΄ μ΄λ κ² ν΄μΌνλ λΌλ μκ°μ΄ λ€μ΄ μ’ μ°Ύμ보λ,
μλ° μμ€μ μ λ κ² μΆκ°νμ§ μκ³ μ€νλ§ μ€μ λ§μΌλ‘λ ν€λμ μΈμ½λ© μ€μ μ κ°λ₯νμ΅λλ€.
λ°λ‘ server.servlet.encoding.charset μ€μ μ μΆκ°ν΄μ£Όλ©΄ λμμ΅λλ€.
νμ λ²μ μμλ μ΄ μ€μ μ΄ spring.http.encoding.charset μΌλ‘ μ¬μ©λμμμ΅λλ€.
μλ° μμ€μμ characterEncodingFilter λΉ μμ±λ‘μ§μ μ κ±°νκ³ , application.yml νμΌμ μλμ κ°μ΄ μ€μ μ μΆκ°ν©λλ€.
server:
servlet:
encoding:
charset: UTF-8
κ·Έλ¦¬κ³ μ±μ μ¬λΆν ν΄μ£Όλ©΄ μλ΅ν€λμ Content-Type κ°μ΄ application/json;charset=utf-8 μΌλ‘ λ³κ²½λ κ²μ νμΈν μ μμ΅λλ€.
λ§μ½ κ°μ΄ λ°λμ§ μλλ€λ©΄ server.servlet.encoding.force-response λλ server.servlet.encoding.force μ€μ κ°μ trueλ‘ μ€μ ν΄μ λ€μ ν΄λ³΄μκΈ° λ°λλλ€.
μ°Έκ³ λ¬Έμ: docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#server.servlet.encoding.charset