JavaMachine
JavaMachine

Reputation: 631

RestController receiving incorrect charset in RequestBody

I have following api:

@ApiOperation(value = "Search product by text")
@PostMapping("/get/search")
public ResponseEntity<List<ShopProductDTO>> get(@RequestBody SearchProductRequestDTO search) {
    //searching product here using search.getSearchText() value
}

Via postman I am sending:

{"searchText":"Утюг"}

But what I am receiving/seeing in logs:

SearchProductRequestDTO{searchText='РЈС‚СРі'}

After enabling DEBUG I see Http11InputBuffer logs where body:

{"searchText":"ГђВЈГ‘<U+0082>Г‘<U+008E>ГђВі"}

What I have done (none of them helped):

  1. Added following properties in application.properties
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.basename=messages
spring.messages.encoding=UTF-8
  1. Exposed CharacterEncodingFilter
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public CharacterEncodingFilter charsetFilter() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    return filter;
}
  1. Executed jar file with -Dfile.encoding=UTF-8 param

  2. Included following headers in Postman

accept-charset:utf-8
content-type:application/json;charset=utf-8

What else I should do? Or am I missing something?

Upvotes: 2

Views: 1127

Answers (2)

Ori Marko
Ori Marko

Reputation: 58774

If the issue is logging, change logger encoding to support UTF-8

#encoding- Over-ride the default character-encoding scheme.
logging.console.encoding=UTF-8

Upvotes: 1

Michael Gantman
Michael Gantman

Reputation: 7792

Try to change your code to this:

@ApiOperation(value = "Search product by text")
@PostMapping(value="/get/search", consumes="application/json;charset=UTF-8")
public ResponseEntity<List<ShopProductDTO>> get(@RequestBody SearchProductRequestDTO search) {
    //searching product here using search.getSearchText() value
}

The change is in your line
@PostMapping(value="/get/search", consumes="application/json;charset=UTF-8")

Upvotes: 1

Related Questions