com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input in Spring Boot App





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















SUMMARY: I send a request from my server application to another endpoint. I get Http 401 error response. When I try to map this error body to my error class, I get an exception like:




com.fasterxml.jackson.databind.JsonMappingException: No content to map
due to end-of-input




DETAILS: I develop a server application with Spring Boot and jackson-datatype-jsr310. I use RestTemplate of Spring framework while sending request to another server application like:



public abstract class AbstractBackendAdapter {

@Autowired
private RestTemplate restTemplate;

@Autowired
private ObjectMapper mapper;

private <T> T exchange(final String url, final HttpMethod method, final HttpEntity request,
final Class<T> responseType) {

...

final ResponseEntity<T> response;

try {
response = restTemplate.exchange(url, method, request, responseType);
} catch (HttpServerErrorException exception) {
throw new BackendHttpServerException(exception, method, url, request.getHeaders());
} catch (HttpClientErrorException exception) {
throw new BackendHttpClientException(exception, method, url, request.getHeaders());
} catch (HttpMessageNotReadableException exception) {
throw new BackendHttpMessageNotReadableException(exception, method, url, request.getHeaders());
}
...
}


I don't use default Spring error handling. Instead of it, I write my exception handler class like:



@RestControllerAdvice
public class RestResponseEntityExceptionHandler {

@Autowired
private ObjectMapper mapper;

@Autowired
private HttpServletRequest httpServletRequest;

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {BackendHttpClientException.class})
@ResponseBody
protected ErrorResponse handleBackendClientException(final BackendHttpClientException exception,
final HttpServletResponse response) {

...

if (HttpStatus.BAD_REQUEST == httpStatus) {
...
} else if (HttpStatus.UNAUTHORIZED == httpStatus) {
final ClientErrorInfo error;

try {
error = mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class);

final String errorCode = error.getErrorCode();

logBackendError(httpMethod, url, headers,
httpStatus, errorCode, error.getErrorMessage(), error.getErrorCause());

errorResponse = errorResponseConverter.prepareErrorResponse(errorCode, response);
} catch (IOException ioException) {
errorResponse = generateUnexpectedJsonFormatError(ioException);
}

}

return errorResponse;
}


However, in some cases, I get Http 401 response with empty body. I mean, exception.getResponseBodyAsString() returns null. Because of that reason, mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class) cannot parse null value to ClientErrorInfo.class. Then, JsonMappingExcepiton is thrown at the line - mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class). Related log is something like:



com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: ; line: 1, column: 0]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3854)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3799)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
at com.commencis.butterfly.core.RestResponseEntityExceptionHandler.handleBackendClientException(RestResponseEntityExceptionHandler.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:384)
at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:59)
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136)
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:74)
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1222)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1034)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:167)
at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)


On the other hand, when I send the same request to the same endpoint via Postman, I get Http 401 error with filled body:



{
"errorCode": "ERR_C13",
"errorMessage": "Authorization failed",
"errorCause": "RETRIEVABLE_PNR_SR_CODE"
}


EDIT-1: By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. Response was something like:



HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=C157382C48E***9972A77DADC; Path=/crane-ticketing; Secure; HttpOnly
Set-Cookie: rememberMe=deleteMe; Path=/crane-ticketing; Max-Age=0; Expires=Tue, 04-Dec-2018 13:38:02 GMT
Content-Type: application/json;charset=UTF-8
Content-Length: 100
Date: Wed, 05 Dec 2018 13:38:01 GMT
Connection: Keep-alive

{"errorCode":"ERR_C13","errorMessage":"Authorization failed","errorCause":"RETRIEVABLE_PNR_SR_CODE"}


EDIT-2: Some spring-jackson configuration lines in the application.properties like:



spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS = false
spring.jackson.deserialization.ADJUST_DATES_TO_CONTEXT_TIME_ZONE = false
spring.jackson.default-property-inclusion=NON_ABSENT
spring.jackson.mapper.AUTO_DETECT_GETTERS=false


How can I see the same error response with filled body in my server application with the error response in Postman? Any ideas will be appreciated!










share|improve this question

























  • Maybe you forgot to add a necessary header to the request like Content-Type: application/json?

    – aBnormaLz
    Jan 3 at 12:21













  • @aBnormaLz By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. You can find details of the response in the question. Content-Type: application/json;charset=UTF-8 is written there. The problem should be something different..

    – séan35
    Jan 3 at 12:39


















1















SUMMARY: I send a request from my server application to another endpoint. I get Http 401 error response. When I try to map this error body to my error class, I get an exception like:




com.fasterxml.jackson.databind.JsonMappingException: No content to map
due to end-of-input




DETAILS: I develop a server application with Spring Boot and jackson-datatype-jsr310. I use RestTemplate of Spring framework while sending request to another server application like:



public abstract class AbstractBackendAdapter {

@Autowired
private RestTemplate restTemplate;

@Autowired
private ObjectMapper mapper;

private <T> T exchange(final String url, final HttpMethod method, final HttpEntity request,
final Class<T> responseType) {

...

final ResponseEntity<T> response;

try {
response = restTemplate.exchange(url, method, request, responseType);
} catch (HttpServerErrorException exception) {
throw new BackendHttpServerException(exception, method, url, request.getHeaders());
} catch (HttpClientErrorException exception) {
throw new BackendHttpClientException(exception, method, url, request.getHeaders());
} catch (HttpMessageNotReadableException exception) {
throw new BackendHttpMessageNotReadableException(exception, method, url, request.getHeaders());
}
...
}


I don't use default Spring error handling. Instead of it, I write my exception handler class like:



@RestControllerAdvice
public class RestResponseEntityExceptionHandler {

@Autowired
private ObjectMapper mapper;

@Autowired
private HttpServletRequest httpServletRequest;

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {BackendHttpClientException.class})
@ResponseBody
protected ErrorResponse handleBackendClientException(final BackendHttpClientException exception,
final HttpServletResponse response) {

...

if (HttpStatus.BAD_REQUEST == httpStatus) {
...
} else if (HttpStatus.UNAUTHORIZED == httpStatus) {
final ClientErrorInfo error;

try {
error = mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class);

final String errorCode = error.getErrorCode();

logBackendError(httpMethod, url, headers,
httpStatus, errorCode, error.getErrorMessage(), error.getErrorCause());

errorResponse = errorResponseConverter.prepareErrorResponse(errorCode, response);
} catch (IOException ioException) {
errorResponse = generateUnexpectedJsonFormatError(ioException);
}

}

return errorResponse;
}


However, in some cases, I get Http 401 response with empty body. I mean, exception.getResponseBodyAsString() returns null. Because of that reason, mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class) cannot parse null value to ClientErrorInfo.class. Then, JsonMappingExcepiton is thrown at the line - mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class). Related log is something like:



com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: ; line: 1, column: 0]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3854)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3799)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
at com.commencis.butterfly.core.RestResponseEntityExceptionHandler.handleBackendClientException(RestResponseEntityExceptionHandler.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:384)
at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:59)
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136)
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:74)
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1222)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1034)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:167)
at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)


On the other hand, when I send the same request to the same endpoint via Postman, I get Http 401 error with filled body:



{
"errorCode": "ERR_C13",
"errorMessage": "Authorization failed",
"errorCause": "RETRIEVABLE_PNR_SR_CODE"
}


EDIT-1: By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. Response was something like:



HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=C157382C48E***9972A77DADC; Path=/crane-ticketing; Secure; HttpOnly
Set-Cookie: rememberMe=deleteMe; Path=/crane-ticketing; Max-Age=0; Expires=Tue, 04-Dec-2018 13:38:02 GMT
Content-Type: application/json;charset=UTF-8
Content-Length: 100
Date: Wed, 05 Dec 2018 13:38:01 GMT
Connection: Keep-alive

{"errorCode":"ERR_C13","errorMessage":"Authorization failed","errorCause":"RETRIEVABLE_PNR_SR_CODE"}


EDIT-2: Some spring-jackson configuration lines in the application.properties like:



spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS = false
spring.jackson.deserialization.ADJUST_DATES_TO_CONTEXT_TIME_ZONE = false
spring.jackson.default-property-inclusion=NON_ABSENT
spring.jackson.mapper.AUTO_DETECT_GETTERS=false


How can I see the same error response with filled body in my server application with the error response in Postman? Any ideas will be appreciated!










share|improve this question

























  • Maybe you forgot to add a necessary header to the request like Content-Type: application/json?

    – aBnormaLz
    Jan 3 at 12:21













  • @aBnormaLz By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. You can find details of the response in the question. Content-Type: application/json;charset=UTF-8 is written there. The problem should be something different..

    – séan35
    Jan 3 at 12:39














1












1








1








SUMMARY: I send a request from my server application to another endpoint. I get Http 401 error response. When I try to map this error body to my error class, I get an exception like:




com.fasterxml.jackson.databind.JsonMappingException: No content to map
due to end-of-input




DETAILS: I develop a server application with Spring Boot and jackson-datatype-jsr310. I use RestTemplate of Spring framework while sending request to another server application like:



public abstract class AbstractBackendAdapter {

@Autowired
private RestTemplate restTemplate;

@Autowired
private ObjectMapper mapper;

private <T> T exchange(final String url, final HttpMethod method, final HttpEntity request,
final Class<T> responseType) {

...

final ResponseEntity<T> response;

try {
response = restTemplate.exchange(url, method, request, responseType);
} catch (HttpServerErrorException exception) {
throw new BackendHttpServerException(exception, method, url, request.getHeaders());
} catch (HttpClientErrorException exception) {
throw new BackendHttpClientException(exception, method, url, request.getHeaders());
} catch (HttpMessageNotReadableException exception) {
throw new BackendHttpMessageNotReadableException(exception, method, url, request.getHeaders());
}
...
}


I don't use default Spring error handling. Instead of it, I write my exception handler class like:



@RestControllerAdvice
public class RestResponseEntityExceptionHandler {

@Autowired
private ObjectMapper mapper;

@Autowired
private HttpServletRequest httpServletRequest;

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {BackendHttpClientException.class})
@ResponseBody
protected ErrorResponse handleBackendClientException(final BackendHttpClientException exception,
final HttpServletResponse response) {

...

if (HttpStatus.BAD_REQUEST == httpStatus) {
...
} else if (HttpStatus.UNAUTHORIZED == httpStatus) {
final ClientErrorInfo error;

try {
error = mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class);

final String errorCode = error.getErrorCode();

logBackendError(httpMethod, url, headers,
httpStatus, errorCode, error.getErrorMessage(), error.getErrorCause());

errorResponse = errorResponseConverter.prepareErrorResponse(errorCode, response);
} catch (IOException ioException) {
errorResponse = generateUnexpectedJsonFormatError(ioException);
}

}

return errorResponse;
}


However, in some cases, I get Http 401 response with empty body. I mean, exception.getResponseBodyAsString() returns null. Because of that reason, mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class) cannot parse null value to ClientErrorInfo.class. Then, JsonMappingExcepiton is thrown at the line - mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class). Related log is something like:



com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: ; line: 1, column: 0]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3854)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3799)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
at com.commencis.butterfly.core.RestResponseEntityExceptionHandler.handleBackendClientException(RestResponseEntityExceptionHandler.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:384)
at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:59)
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136)
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:74)
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1222)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1034)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:167)
at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)


On the other hand, when I send the same request to the same endpoint via Postman, I get Http 401 error with filled body:



{
"errorCode": "ERR_C13",
"errorMessage": "Authorization failed",
"errorCause": "RETRIEVABLE_PNR_SR_CODE"
}


EDIT-1: By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. Response was something like:



HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=C157382C48E***9972A77DADC; Path=/crane-ticketing; Secure; HttpOnly
Set-Cookie: rememberMe=deleteMe; Path=/crane-ticketing; Max-Age=0; Expires=Tue, 04-Dec-2018 13:38:02 GMT
Content-Type: application/json;charset=UTF-8
Content-Length: 100
Date: Wed, 05 Dec 2018 13:38:01 GMT
Connection: Keep-alive

{"errorCode":"ERR_C13","errorMessage":"Authorization failed","errorCause":"RETRIEVABLE_PNR_SR_CODE"}


EDIT-2: Some spring-jackson configuration lines in the application.properties like:



spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS = false
spring.jackson.deserialization.ADJUST_DATES_TO_CONTEXT_TIME_ZONE = false
spring.jackson.default-property-inclusion=NON_ABSENT
spring.jackson.mapper.AUTO_DETECT_GETTERS=false


How can I see the same error response with filled body in my server application with the error response in Postman? Any ideas will be appreciated!










share|improve this question
















SUMMARY: I send a request from my server application to another endpoint. I get Http 401 error response. When I try to map this error body to my error class, I get an exception like:




com.fasterxml.jackson.databind.JsonMappingException: No content to map
due to end-of-input




DETAILS: I develop a server application with Spring Boot and jackson-datatype-jsr310. I use RestTemplate of Spring framework while sending request to another server application like:



public abstract class AbstractBackendAdapter {

@Autowired
private RestTemplate restTemplate;

@Autowired
private ObjectMapper mapper;

private <T> T exchange(final String url, final HttpMethod method, final HttpEntity request,
final Class<T> responseType) {

...

final ResponseEntity<T> response;

try {
response = restTemplate.exchange(url, method, request, responseType);
} catch (HttpServerErrorException exception) {
throw new BackendHttpServerException(exception, method, url, request.getHeaders());
} catch (HttpClientErrorException exception) {
throw new BackendHttpClientException(exception, method, url, request.getHeaders());
} catch (HttpMessageNotReadableException exception) {
throw new BackendHttpMessageNotReadableException(exception, method, url, request.getHeaders());
}
...
}


I don't use default Spring error handling. Instead of it, I write my exception handler class like:



@RestControllerAdvice
public class RestResponseEntityExceptionHandler {

@Autowired
private ObjectMapper mapper;

@Autowired
private HttpServletRequest httpServletRequest;

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {BackendHttpClientException.class})
@ResponseBody
protected ErrorResponse handleBackendClientException(final BackendHttpClientException exception,
final HttpServletResponse response) {

...

if (HttpStatus.BAD_REQUEST == httpStatus) {
...
} else if (HttpStatus.UNAUTHORIZED == httpStatus) {
final ClientErrorInfo error;

try {
error = mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class);

final String errorCode = error.getErrorCode();

logBackendError(httpMethod, url, headers,
httpStatus, errorCode, error.getErrorMessage(), error.getErrorCause());

errorResponse = errorResponseConverter.prepareErrorResponse(errorCode, response);
} catch (IOException ioException) {
errorResponse = generateUnexpectedJsonFormatError(ioException);
}

}

return errorResponse;
}


However, in some cases, I get Http 401 response with empty body. I mean, exception.getResponseBodyAsString() returns null. Because of that reason, mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class) cannot parse null value to ClientErrorInfo.class. Then, JsonMappingExcepiton is thrown at the line - mapper.readValue(exception.getResponseBodyAsString(), ClientErrorInfo.class). Related log is something like:



com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: ; line: 1, column: 0]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3854)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3799)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
at com.commencis.butterfly.core.RestResponseEntityExceptionHandler.handleBackendClientException(RestResponseEntityExceptionHandler.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:384)
at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:59)
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136)
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:74)
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1222)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1034)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:167)
at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)


On the other hand, when I send the same request to the same endpoint via Postman, I get Http 401 error with filled body:



{
"errorCode": "ERR_C13",
"errorMessage": "Authorization failed",
"errorCause": "RETRIEVABLE_PNR_SR_CODE"
}


EDIT-1: By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. Response was something like:



HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=C157382C48E***9972A77DADC; Path=/crane-ticketing; Secure; HttpOnly
Set-Cookie: rememberMe=deleteMe; Path=/crane-ticketing; Max-Age=0; Expires=Tue, 04-Dec-2018 13:38:02 GMT
Content-Type: application/json;charset=UTF-8
Content-Length: 100
Date: Wed, 05 Dec 2018 13:38:01 GMT
Connection: Keep-alive

{"errorCode":"ERR_C13","errorMessage":"Authorization failed","errorCause":"RETRIEVABLE_PNR_SR_CODE"}


EDIT-2: Some spring-jackson configuration lines in the application.properties like:



spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS = false
spring.jackson.deserialization.ADJUST_DATES_TO_CONTEXT_TIME_ZONE = false
spring.jackson.default-property-inclusion=NON_ABSENT
spring.jackson.mapper.AUTO_DETECT_GETTERS=false


How can I see the same error response with filled body in my server application with the error response in Postman? Any ideas will be appreciated!







java spring error-handling jackson jackson-databind






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 8 at 7:39







séan35

















asked Jan 3 at 12:19









séan35séan35

402722




402722













  • Maybe you forgot to add a necessary header to the request like Content-Type: application/json?

    – aBnormaLz
    Jan 3 at 12:21













  • @aBnormaLz By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. You can find details of the response in the question. Content-Type: application/json;charset=UTF-8 is written there. The problem should be something different..

    – séan35
    Jan 3 at 12:39



















  • Maybe you forgot to add a necessary header to the request like Content-Type: application/json?

    – aBnormaLz
    Jan 3 at 12:21













  • @aBnormaLz By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. You can find details of the response in the question. Content-Type: application/json;charset=UTF-8 is written there. The problem should be something different..

    – séan35
    Jan 3 at 12:39

















Maybe you forgot to add a necessary header to the request like Content-Type: application/json?

– aBnormaLz
Jan 3 at 12:21







Maybe you forgot to add a necessary header to the request like Content-Type: application/json?

– aBnormaLz
Jan 3 at 12:21















@aBnormaLz By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. You can find details of the response in the question. Content-Type: application/json;charset=UTF-8 is written there. The problem should be something different..

– séan35
Jan 3 at 12:39





@aBnormaLz By using Charles, I intervened between my server app and the endpoint that I send a request to and checked the response before my server app received. You can find details of the response in the question. Content-Type: application/json;charset=UTF-8 is written there. The problem should be something different..

– séan35
Jan 3 at 12:39












1 Answer
1






active

oldest

votes


















0














After I made some debugging, I realized that the cause of the problem is code in SimpleClientHttpResponse that is a default response class of Resttemplate. Also, I found that there are some issues about this problem in Spring JIRA:



Reference: https://jira.spring.io/browse/SPR-9367



"It is very difficult (impossible) to handle a 401 response in the RestTemplate with default settings. In fact it is possible, but you have to supply an error handler and a request factory. The error handler was obvious, but the problem is that the default request factory uses java.net which can throw HttpRetryException when you try to look at the status code of the response (despite it being obviously available). The solution is to use HttpComponentsClientHttpRequestFactory."



When I added the dependency written below in my POM file, my application started to handle the body of 401 response.



<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022183%2fcom-fasterxml-jackson-databind-jsonmappingexception-no-content-to-map-due-to-en%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    After I made some debugging, I realized that the cause of the problem is code in SimpleClientHttpResponse that is a default response class of Resttemplate. Also, I found that there are some issues about this problem in Spring JIRA:



    Reference: https://jira.spring.io/browse/SPR-9367



    "It is very difficult (impossible) to handle a 401 response in the RestTemplate with default settings. In fact it is possible, but you have to supply an error handler and a request factory. The error handler was obvious, but the problem is that the default request factory uses java.net which can throw HttpRetryException when you try to look at the status code of the response (despite it being obviously available). The solution is to use HttpComponentsClientHttpRequestFactory."



    When I added the dependency written below in my POM file, my application started to handle the body of 401 response.



    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    </dependency>





    share|improve this answer




























      0














      After I made some debugging, I realized that the cause of the problem is code in SimpleClientHttpResponse that is a default response class of Resttemplate. Also, I found that there are some issues about this problem in Spring JIRA:



      Reference: https://jira.spring.io/browse/SPR-9367



      "It is very difficult (impossible) to handle a 401 response in the RestTemplate with default settings. In fact it is possible, but you have to supply an error handler and a request factory. The error handler was obvious, but the problem is that the default request factory uses java.net which can throw HttpRetryException when you try to look at the status code of the response (despite it being obviously available). The solution is to use HttpComponentsClientHttpRequestFactory."



      When I added the dependency written below in my POM file, my application started to handle the body of 401 response.



      <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      </dependency>





      share|improve this answer


























        0












        0








        0







        After I made some debugging, I realized that the cause of the problem is code in SimpleClientHttpResponse that is a default response class of Resttemplate. Also, I found that there are some issues about this problem in Spring JIRA:



        Reference: https://jira.spring.io/browse/SPR-9367



        "It is very difficult (impossible) to handle a 401 response in the RestTemplate with default settings. In fact it is possible, but you have to supply an error handler and a request factory. The error handler was obvious, but the problem is that the default request factory uses java.net which can throw HttpRetryException when you try to look at the status code of the response (despite it being obviously available). The solution is to use HttpComponentsClientHttpRequestFactory."



        When I added the dependency written below in my POM file, my application started to handle the body of 401 response.



        <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        </dependency>





        share|improve this answer













        After I made some debugging, I realized that the cause of the problem is code in SimpleClientHttpResponse that is a default response class of Resttemplate. Also, I found that there are some issues about this problem in Spring JIRA:



        Reference: https://jira.spring.io/browse/SPR-9367



        "It is very difficult (impossible) to handle a 401 response in the RestTemplate with default settings. In fact it is possible, but you have to supply an error handler and a request factory. The error handler was obvious, but the problem is that the default request factory uses java.net which can throw HttpRetryException when you try to look at the status code of the response (despite it being obviously available). The solution is to use HttpComponentsClientHttpRequestFactory."



        When I added the dependency written below in my POM file, my application started to handle the body of 401 response.



        <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        </dependency>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 11 at 8:36









        séan35séan35

        402722




        402722
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022183%2fcom-fasterxml-jackson-databind-jsonmappingexception-no-content-to-map-due-to-en%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory