mercredi 25 février 2015

How to intercept HttpServletRequest with Spring MVC Jackson

I'm using Spring/Jackson to auto convert json to POJOs. All is working fine, except when I'm doing my header authentication (using a filter). I've been using request.getContentLength() to get the length of the json string.


This worked fine, until the json contains a diacritic character. Where the content length then reports to be one character longer. So obviously I have to get the actual json body. This is proving difficult as calling request.getInputStream causes Jackson to fail as the input stream is already closed. Same for getReader.


So, I've done as outlined in this blog: http://ift.tt/18lxuSj


Which works but doesn't properly encode to UTF-8. So I replaced this line:



bufferedReader = new BufferedReader(new InputStreamReader(inputStream));


to:



bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));


Which displays the correct json in my log, but I get this error when jackson tries to convert to a pojo:



nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0x9f


I'm not sure how to get this working, if anybody has any ideas?


Aucun commentaire:

Enregistrer un commentaire