简体   繁体   中英

SpringBoot - HttpMessageNotReadableException: JSON parse error: java.net.SocketTimeoutException

My code is throwing an exception as follows

JSON parse error: java.net.SocketTimeoutException; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.net.SocketTimeoutException (through reference chain: java.util.ArrayList[9])
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: java.net.SocketTimeoutException; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.net.SocketTimeoutException (through reference chain: java.util.ArrayList[9])
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:245) ~[spring-web-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227) ~[spring-web-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:204) ~[spring-webmvc-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157) ~[spring-webmvc-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130) ~[spring-webmvc-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:127) ~[spring-web-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) ~[spring-web-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) ~[spring-web-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) ~[spring-webmvc-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) [spring-webmvc-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]

My spring-boot version is 2.1.7.RELEASE and the embedded tomcat is used to run the application. I also set the property server.connection-timeout=30s

I almost search everywhere and couldn't identify the root cause of this problem. I am also catching this exception and printing it in the log and return the response as BAD_REQUEST.

This exception is throwing intermittently and my production server consumes above 80% of CPU and memory at this time.

This is actually a POST request and the request body is validated as follows

    @RequestMapping( value = "/save", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE )
    public SaveDto create( @RequestBody @Valid SaveDto saveDto,
        HttpServletResponse response, HttpServletRequest request )
    {
      //my logic
    }

and my SaveDto is as follows

@JsonInclude( JsonInclude.Include.NON_NULL )
public class SaveDto
{
    private String id;
    @Size( min = 1 )
    @Valid
    private List< SaveEntryDto > saveLogEntries;
}

My saveEntryDto is as follows

public class SaveEntryDto implements Comparable<SaveEntryDto> {
    private String id;
    private String idRef;
    private WER wer;
    @Min(0)
    @Max(1440)
    private Integer kjh;
    @Min(0)
    @Max(1440)
    private Integer gfd;
    private String abc;
    private Double def;
    private String ghi;
    private Boolean klm = Boolean.FALSE;
    private Boolean hug;
    private Boolean qwe;
    private Double azx;
    private Double xds;
    private Boolean cvf = Boolean.FALSE;
    private Boolean bgh = Boolean.FALSE;
    private String nmj;
    private Boolean rgh;
    private Boolean jkh;
    private Boolean mkl = Boolean.FALSE;
    private Boolean wed = Boolean.FALSE;
    private Long ftu;
    private XYZ xyzxd;
    private String klp;
    private ABC xxz;
    private Boolean llo;
    private Long iop;
    private Double poi;
    private Boolean uyt = Boolean.FALSE;
    private Boolean rew = Boolean.FALSE;
    private String qsd;
}

Try server.tomcat.connection-timeout instead of server.connection-timeout . For some reason there are many mentions of server.connection-timeout , but I don't think it does anything (according to https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html ).

server.tomcat.connection-timeout=30000 - this sets the timeout to 30s

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