I have a SpringBoot 2.6.14 web-app, written in java8, and I have to realize a download of clob
that is inside my database.
Usually, to export files, I use rest-controller like follows:
@GetMapping("/upload/{id}")
@ApiResponse(responseCode = "200", description = "OK",content = @Content (schema = @Schema (type = "string", format = "byte")))
public ResponseEntity<byte[]> getUpload(@PathVariable("id") Integer id)
throws Exception {
try {
byte[] result = someService.convertClobToByte(id);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=" + "upload.xlsx");
return ResponseEntity.ok()
.headers(headers)
.contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
.body(result);
} catch (ServiceException se) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, se.getMessage(), se);
}
}
Now the customer ask me to set a time limit. if the download takes more than 30 seconds I have to thrown an Exception and block tthe operation.
Now he said me that to create a custom restTemplate like follow I found googling around:
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.setConnectTimeout(...)
.setReadTimeout(...)
.build();
}
}
Now I don't know how to use it inside my controller.. Usually I use restTemplate to call API on other machine using, for example, the exchange
method.
What I should place as URI
or entity
inside my exchange method? Maybe should I use this other version of snippet found here:
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(2000);
factory.setConnectTimeout(2000);
return factory;
}
}
Will the above code apply the timeout to all HTTP Connection? What is the best way to do this kind of job?