@Override public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { TumblrResponse tumblrResponse = objectMapper.readValue(inputMessage.getBody(), TumblrResponse.class); checkResponse(tumblrResponse); Object result; if (TumblrResponse.class.equals(type)) { // don't parse the response json, callee is going to process is manually result = tumblrResponse; } else { // parse the response json into an instance of the given class JavaType javaType = getJavaType(type, contextClass); String response = tumblrResponse.getResponseJson(); result = objectMapper.readValue(response, javaType); } return result; } catch (JsonParseException ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); } catch (EOFException ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); } catch (Exception e) { e.printStackTrace(); throw new IOException(e); } }
protected void checkResponse(TumblrResponse tumblrResponse) { HttpStatus httpStatus = HttpStatus.valueOf(tumblrResponse.getStatus()); if (httpStatus.series() == HttpStatus.Series.CLIENT_ERROR) { throw new HttpClientErrorException(httpStatus, tumblrResponse.getMessage()); } else if (httpStatus.series() == HttpStatus.Series.SERVER_ERROR) { throw new HttpServerErrorException(httpStatus, tumblrResponse.getMessage()); } }