Announcement Announcement Module
Collapse
No announcement yet.
EOFException: No content to map to Object due to end of input Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • EOFException: No content to map to Object due to end of input

    Need to read JSON request payload in my Custom HandlerInterceptorAdapter class to check for a key "token". If all is fine , the dedicated handler should then service the incoming request.

    My HandlerInterceptorAdapter is as follows :

    Code:
    public class RequestInterceptor extends HandlerInterceptorAdapter{
    	
    	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
    		
    		String requestUri = request.getRequestURI();
    		
    		if(requestUri.endsWith("login") ) {	
     		 			
     			return true;
     			
    		} else {
    			
    			JSONParser jsonParser = new JSONParser();
    			
    			CustomRequestWrapper customRequestWrapper = new CustomRequestWrapper(request);
    			
    			JSONObject reqJsonObject = (JSONObject) jsonParser.parse( customRequestWrapper.getBody() );
    					
    			if ( missing token ) {
    				sendErrorResponse(response,"Missing Token");
    				
    				return false;
    			}
    			
    			else {
    		
    				// all fine. forward to dedicated handler
    				return super.preHandle(customRequestWrapper, response, handler); 
    			}
    			
    		}
    
    	}
    My CustomRequestWrapper class is as follow :


    Code:
    public class CustomRequestWrapper  extends HttpServletRequestWrapper {
    	
            private final String requestBody;
    	
    	public CustomRequestWrapper  (HttpServletRequest request) throws IOException {
    		
    		super(request);
    		
    		requestBody = IOUtils.toString( request.getInputStream() );
    			
    	}
    
    	public ServletInputStream getInputStream() throws IOException {
    		
    		 final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody.getBytes());
    		 
    		 ServletInputStream servletInputStream = new ServletInputStream() {
    		     public int read() throws IOException {
    		       return byteArrayInputStream.read();
    		     }
    		   };
      
    		 return servletInputStream;
    	}
    	
    	 @Override
    	 public BufferedReader getReader() throws IOException {
    	   return new BufferedReader(new InputStreamReader(this.getInputStream()));
    	 }
    	 
    	 public String getBody() {	 
    	   return this.requestBody;
    	 }
    }
    If the token is not missing the request payload does not get forwarded to dedicated handler .

    Stack Trace is as follows:

    Code:
    07:46:17,983 DEBUG org.springframework.web.servlet.DispatcherServlet.processRequest:910 - Could not complete request
    java.io.EOFException: No content to map to Object due to end of input
    	at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2775)
    	at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718)
    	at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1923)
    	at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:124)
    	at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:153)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:641)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:605)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:354)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:195)
    	at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:266)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    	at java.lang.Thread.run(Thread.java:662)
    Kindly help resolve this issue.

  • #2
    Did you get a solution to this, I am currently having the exact same issue?

    Comment

    Working...
    X