すきま風

勉強したことのメモとか

Spring Boot の Web Applicationで Shift_Jis (あるいはUTF-8以外) のリクエストを受け取る

方法

application.properties以下のように設定する

spring.http.encoding.force=false

または

spring.http.encoding.force-request=false 

解説

Spring MVCはCharacterEncodingFilterでHttpServletRequest, HttpServletResponseのCharacterEncodingをoverrideする。

Spring Bootの場合、application.properties に適切に設定を入れておかないと、 clientの設定したCharacterEncodingをガン無視してHttpServletRequestのCharacterEncodingをUTF-8に上書きするストロングスタイルになってしまうのである 😑


application.properties result
forceRequest: true forceResponse: false
spring.http.encoding.force=true forceRequest: true forceResponse: true
spring.http.encoding.force=false forceRequest: false forceResponse: false
spring.http.encoding.force-request=false forceRequest: false forceResponse: false


2020年になって文字化け問題にぶつかるとは思わなかった 😥

参考

Selectively force encoding in CharacterEncodingFilter · spring-projects/spring-framework@76ee468 · GitHub