Good afternoon, I hope you are having a good day and you can help me with the following problem ....
I have the following resource:
@RequestMapping(value = "/dowload", method = RequestMethod.GET,produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE})
public ResponseEntity<Resource> downloadPDFFile()
throws IOException {
File file = new File("/home/recursos/archivos.pdf");
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
Path path = Paths.get(file.getAbsolutePath());
ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));
return ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
}
I try to access it in the following way
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<Resource> response = clientAccess.exchange("http://localhost:8081/springjwt/dowload", HttpMethod.GET, entity, Resource.class);
But I get the following error
Could not read JSON: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1b318862; line: 1, column: 2]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1b318862; line: 1, column: 2]
Use Spring Boot to implement the REST server.
Thanks for the help you can give me.
Thanks for your response, but the error persists. I add another part of the code that I think is important and possibly this is setting something wrong.
public OAuth2RestTemplate clientAccess(String user, String password) {
ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();
resourceDetails.setUsername(user);
resourceDetails.setPassword(password);
resourceDetails.setAccessTokenUri("http://localhost:8081/oauth/token");
resourceDetails.setClientId("testjwtclientid");
resourceDetails.setClientSecret("XY7kmzoNzl100");
resourceDetails.setGrantType("password");
resourceDetails.setScope(asList("read", "write"));
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new StringHttpMessageConverter());
converters.add(mappingJackson2HttpMessageConverter);
converters.add(new ByteArrayHttpMessageConverter());
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
restTemplate.setMessageConverters(converters);
System.out.println(restTemplate.getAccessToken().getValue());
//final Object greeting = restTemplate.getForObject("http://localhost:8081/springjwt/cities", Object.class);
System.out.println("");
//System.out.println(greeting);
return restTemplate;
}
What I returned as an answer I use to make the subsequent requests, I make a first to get the user's data and do it correctly, and when I try to download the file is when I get the error.