Skip to content

Character encoding is reset when exceptions are handled in MVC #36142

@a96219

Description

@a96219

When a global exception is caught via @RestControllerAdvice and a ResponseEntity is returned,
HttpServletResponse.getCharacterEncoding() returns UTF-8 in Spring Boot 3,
but returns ISO-8859-1 in Spring Boot 4.

springboot 3 config:

server:
  servlet:
    encoding:
      charset: utf-8
      enabled: true
      force-response: true

springboot4 config:

spring:
  servlet:
    encoding:
      charset: utf-8
      enabled: true
      force-response: true

Minimal example program:

Steps to run:

  1. Start the Spring Boot application by running the com.lwd.Main.main() method
  2. Access the endpoint http://127.0.0.1:8080/test

In Spring Boot 3, the following log can be seen in the console:

2026-01-14T02:03:10.342+08:00  INFO 137476 --- [nio-8080-exec-1] com.lwd.LogFilter                        : charSet:UTF-8

In Spring Boot 4, the following log can be seen in the console:

2026-01-14T02:03:38.296+08:00  INFO 118412 --- [nio-8080-exec-2] com.lwd.LogFilter                        : charSet:ISO-8859-1

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions