Download file from REST

0

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.

    
asked by Kote 23.12.2017 в 20:34
source

1 answer

0

Seeing the new information that you have uploaded, I can tell you that the error is in your clientAccess method. You are telling jackson that the MappingJackson2HttpMessageConverter converter that is used to write and read JSON, use it to work with bytes which is what MediaType.APPLICATION_OCTET_STREAM_VALUE returns.

I add the method with the change made.

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 ByteArrayHttpMessageConverter());
    //converters.add(new StringHttpMessageConverter());
    converters.add(new ResourceHttpMessageConverter());
    //converters.add(new SourceHttpMessageConverter<>());
    //converters.add(new AllEncompassingFormHttpMessageConverter());
    //converters.add(new MappingJackson2HttpMessageConverter());
    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;
}

If you look closely, I just left a converter that is the correct one for what your service returns, however by default Spring will configure all the other converters that are commented in the code, so if you do not add any converter jackson already the should have aggregates: Method restTemplate.getMessageConverters() shows you all converters already added.

    
answered by 24.12.2017 / 06:10
source