Spring boot REST API returns partial response - spring

I have an Spring boot REST API that returns around 1GB of JSON output. However, the application stops returning results after 5mins.
I had already set the timeout values in different iterations with no success.
server:
connection-timeout: 50000000
session:
timeout: 1000000
servlet:
session:
timeout: 1000s
I have also set the timeout in the rest template to 10 mins and the API returns partial response with rest template and results in JSON parse error.
Spring boot version : 2.2.0.RELEASE
I looked at the other stack overflow answers. None of them seem to be working.
I had also tried in curl and it stops with the error
curl: (18) transfer closed with outstanding read data remaining
In Spring boot, it results in unable to parse the JSON response error as the response was not complete.

Related

Graphql and rest api on same spring boot application causes wrong response codes

I have a spring boot application that both have normal REST controllers and it also works as a GraphQL server.
Some time ago my DB crashed and I had
datasource:
hikari:
maximum-pool-size: 10
minimum-idle: 1
connection-timeout: 60000
So all requests to my server failed after 60 seconds cause they couldn't open a transaction. The return code in all GraphQL responses is 200 ok. And that's fine. But if my REST APIs fail they should return an error like 500 internal errors which they didn't. They also returned 200 OK.
Has anyone else experienced something similar?
I'm thinking it has something to do with the GraphQL dependency
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>12.0.0</version>
</dependency>
I haven't found anything that explains it yet and I haven't been able to reproduce the error.

Handle Tomcat aborting file upload in Spring Boot

Is there a way to handle case when Tomcat aborts file upload due to defined limits, in order to return decent response (for example 413 Payload Too Large)?
I'm using Spring Boot 2.4.0 with following configuration:
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=20MB
spring.servlet.multipart.max-request-size=21MB
server.tomcat.max-swallow-size=21MB
server.tomcat.max-http-form-post-size=20MB
When trying to upload files larger than 21MB (or 26MB for some reason) using REST API, I receive java.net.SocketException: Connection reset by peer because Tomcat aborts the upload (closes the socket), due to defined maxSwallowSize. There's no exception in the logs by Spring. When running tests, I can see that RestTemplate retries couple of times, but eventually throws exception.
You can find MWE on GitHub.

SpringBoot application giving RestClientException (Unknown status code [494] null) 2 days after deploying to EC2 instance

Our application is a spring batch application running on EC2.
We have a RestTemplate call to an external application and it works fine.
But after 2 days, it started to fail.
we checked with the external application and logs shows the rest service response was successful from their end.
But our application log shows the below exception.
We tried redeploying our application and this behaviour still persists(works for 2 days and fails after that).
We are using spring boot 1.5.9 version.
2019-07-04 19:50:38.078 [10.174.81.249:SimpleAsyncTaskExecutor-2380]
Exception when getting data : org.springframework.web.client.UnknownHttpStatusCodeException: Unknown status code [494] null
at org.springframework.web.client.DefaultResponseErrorHandler.getHttpStatusCode(DefaultResponseErrorHandler.java:88)
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:48)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:688)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
I found status code 494 Request header too large is used by nginx and spring has not covered this status as it is not from the standard.
Please look enum org.springframework.http.HttpStatus
Also org.springframework.web.client.DefaultResponseErrorHandler
Solution: You have to check your configuration to verify which header is too large.

feign.RetryableException: Read timed out executing GET

I have below architecture in my project
My UI Service(Port 8080) making Feign call to Gateway Service(Port 8085).
My Get call from UI service is " http://localhost:8080/invoice-list?startDate=2018-08-05&endDate=2018-10-05 "
Similar call from Gateway Service "http://localhost:8085/invoice-download-service/invoice-list?startDate=2018-08-05&endDate=2018-10-05"
When i make this GET call from UI service i get below error within minute
is feign.RetryableException: Read timed out executing GET http://localhost:8085/invoice-download-service/invoice-list?startDate=2018-08-05&endDate=2018-10-05] with root cause
java.net.SocketTimeoutException: Read timed out
But when i make direct call from Gateway Server to microservice, i dont get error.
Application.properties file of Gateway service
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=160000000
ribbon.OkToRetryOnAllOperations=true
ribbon.ReadTimeout=5000000
ribbon.ConnectTimeout=5000000
ribbon.MaxAutoRetries=3
ribbon.MaxAutoRetriesNextServer=3
zuul.host.socket-timeout-millis= 5000000
zuul.host.connect-timeout-millis= 5000000
Here i have set readtimeout and connecttimeout property around 8 to 10 min, hence i am not getting error.
Application.properties file of UI service
spring.application.name=external-ui-service
server.port=8080
Here in UI service i dont have timeout property. I tried above properties here but not working.
Obviously this UI service is not using ribbon,zuul etc. This is just an making Feign call to gateway.
So what should i do to increase timeout in UI service?
Added below properties in UI Service's application.propeties file.
feign.client.config.default.connectTimeout: 160000000
feign.client.config.default.readTimeout: 160000000
This issue might also be caused by default laodbalancer implementation of Spring Cloud Gateway in case you make use of Eureka Server and run your microservices undockerized on windows. Services are running on localhost, but Eureka says to the loadbalancer of the gateway to route the request to host.docker.internal.
The links down below give a couple of solutions:
https://localcoder.org/spring-boot-cloud-eurka-windows-10-eurkea-returns-host-docker-internal-for-clien
https://dimitr.im/fix-eureka-localhost

spring actuator increases the metric counter.status.200.{my endpoint} even if the endpoint returns a 500

I've developed my first rest service with Spring Boot, Spring MVC Spring Actuator using embedded tomcat 8.
For some reason, when the endpoint fails due to an exception which is not caught, the returned response from the endpoint has the status 500, but the metric of counter.status.200. was increased.
I debug a bit the code and looks like the status of the response in the class ResponseFacade (from tomcat) is set after the metric is increased on the MetricFilterAutoConfiguration.MetricsFilter.
Does someone has an idea how can I get the right status code counter (counter.status.500.{my endpoint}) increased?
Thanks in advance

Resources