简体   繁体   中英

Handle JSON parse error in Jersey

How can I intercept a parse error in my Jersey REST application? I already registered an ExceptionMapper<Exception> but those don't get caught.

It returns the following to the client

Unexpected character ('y' (code 121)): expected a valid value (number,
  String, array, object, 'true', 'false' or 'null')
  at [Source: org.apache.catalina.connector.CoyoteInputStream@161d5f3;
  line: 1, column: 92]

But I want to send my own Response.

I'm guessing however your ExceptionMapper is registered, it isn't being applied to other packages, where you're invoking the Jersey library code to parse JSON.

Surround all calls to Jersey for parsing in a Try{ }catch() block, where the catch should return an error code and not process further.

Alternatively, it looks like Tomcat might be throwing the error before it even gets to your application. Maybe an issue with the way it is configured, or the way the client is communicating? Is this plaintext HTTP?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM