got exception when deserializing non ASCII HTTP request body to enum type by Jackson in springmvc

130 views
Skip to first unread message

Wen Jian Liang

unread,
Jan 24, 2021, 8:06:18 PM1/24/21
to jackson-user
Hi there,
I am using Jackson in my java/spring-boot/spring MVC project, and I have some enum whose values are not ASCII like this:

```
public enum Type {
  数值, 日期, 文本
}
```

when I POST or PUT an HTTP request with these enum's value as body only, like this:

```
POST /some-url
Content-Type: application/json;charset=UTF-8
...(some other request headers)

文本
```

and in the same time, I use the enum type as the param of Controller method to receive the @RequestBody that is deserialized from Jackson, like this:

```
@RequestMapping("some-url")
public void method(@RequestBody Type type){
}
```

then Jackson will throw this exception in spring MVC:

```
Caused by: com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 start byte 0x96
  at [Source: (PushbackInputStream); line: 1, column: 3]
  at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
  at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:693)
  at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidInitial(UTF8StreamJsonParser.java:3545)
  at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._decodeCharForError(UTF8StreamJsonParser.java:3288)
  at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3520)
  at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2627)
  at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:832)
  at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:729)
  at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4141)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3084)
  at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:239)
```

but if I quote the request body with the quote mark("), like this:
```
POST /some-url
Content-Type: application/json;charset=UTF-8
...(some other request headers)

"文本"
```
that will be ok.
or if the enum values are ASCII, that will be ok too.
or if I use a String type object to receive the @RequestBody param, that will be ok too.

so, is there some advice for my trouble?

hope to reply and thank you!

Tatu Saloranta

unread,
Jan 24, 2021, 8:14:29 PM1/24/21
to jackson-user
It is possible that either request is not encoded in UTF-8 like POST request header claims (and if so, sender needs to fix this, either  by specifying correct encoding or making sure content is in UTF-8), or, possibly, that content is wrong -- for example, Enum value is sent without quotes and is not valid JSON value. In the latter case the reported error may be misleading.

If it is the latter case, what would be needed to help would be a reproduction like unit test, without using external web framework (that is, only using Jackson functionality).

-+ Tatu +-


 
Reply all
Reply to author
Forward
0 new messages