1

I use Spring MVC, Tomcat 8.5.81 and java-configured context. Here is the controller class. If I send ResponseEntity<?> with Cyrillic from here, everything is represented on the client correctly.

@RestController
@RequestMapping(value = "/rooms", produces = {"application/json; charset=UTF-8"})
@RequiredArgsConstructor
public class RoomController {

However, if I use my @ControllerAdvice to send custom exception with Cyrillic description on the client I got ?????? symbols.

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(value = {
        NoSuchElementException.class,
        IllegalArgumentException.class,
        ServiceUnavailableException.class})
    protected ResponseEntity<Object> handleConflict(RuntimeException ex, WebRequest req) {
    String responseBody = ex.getMessage();
    return handleExceptionInternal(ex, responseBody, new HttpHeaders(), HttpStatus.BAD_REQUEST, req);
    }
}

This is a part of my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>11</java.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <hibernate.version>5.6.8.Final</hibernate.version>
</properties>

How to set encoding UTF-8 to @ControllerAdvice/@ExceptionHandler?

cognosce
  • 11
  • 4

0 Answers0