5

i want to perform a get request on a server hosted on localhost:80 (for example but could be every host) from my spring boot application hosted on localhost:8080.

For example i want to get an image hosted on locahost:80/image.jpg from my spring application. How can i handle this?

coman
  • 79
  • 1
  • 1
  • 6

3 Answers3

7

You can use RestTemplate for that.

        RestTemplate restTemplate = new RestTemplate();

        String uri = localhost:80; // or any other uri

        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");

        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
        ResponseEntity<?> result =
                restTemplate.exchange(uri, HttpMethod.GET, entity, returnClass);
        return result.getBody();

If you want to get images then use following method:

String url = "http://img.championat.com/news/big/l/c/ujejn-runi_1439911080563855663.jpg";
byte[] imageBytes = restTemplate.getForObject(url, byte[].class);
Files.write(Paths.get("image.jpg"), imageBytes);

You will also need to configure ByteArrayHttpMessageConverter in application config:

@Bean
public RestTemplate restTemplate(List<HttpMessageConverter<?>> messageConverters) {
    return new RestTemplate(messageConverters);
}

@Bean
public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
    return new ByteArrayHttpMessageConverter();
}
Drashti Dobariya
  • 2,455
  • 2
  • 10
  • 23
  • This return a Json/string format, what if i want an image instead? – coman Jun 15 '21 at 13:43
  • And i bet i should even change the signature of restTemplate.exchange. The third parametr is a "returnClass", what i should change for image? – coman Jun 15 '21 at 14:00
  • Sorry i keep not understing. Where it is saving the image? Where i can edit "application config" – coman Jun 15 '21 at 14:55
  • It will save image in the Path that you will specify in Paths.get() and create a new java class and annotate it with @Configuration. Any Beans that you put inside that will be added to application config. – Drashti Dobariya Jun 16 '21 at 03:15
2

If you want to send a request with spring you can do

//first create e restemplate variable
RestTemplate restTemplate=new RestTemplate();

//you can create and edit header
HttpHeaders header= new HttpHeaders();
header.add("Authorization", "*****************");
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
header.add("Accept", "application/json");

//you can create and edit body to
MultiValueMap<String, String> body= new LinkedMultiValueMap<String, String>();
body.add("grant_type", "client_credentials");

HttpEntity<MultiValueMap<String, String>> requeteHttp =new HttpEntity<MultiValueMap<String, String>>(body, header);

//After you can create a request
ResponseEntity<Response_class> reponse = restTemplate.postForEntity("your api link", requeteHttp , Response_class.class);
//if you want to send a get request you can edit postForEntity to get

About Response_class if you know the return type of the request, you can create a class and use it here, otherwise you can use string instead

if your request returns a json like this

{
    "token_type":"***",
    "access_token":"***",
    "expires_in":"***",
}

you can create a Response_class controller(class) and call it like we did above otherwise you can use string instead

public class Response_class{
    private String token_type;
    private String access_token;
    private String expires_in;

    public Response_class(String token_type, String access_token, String expires_in) {
        this.token_type = token_type;
        this.access_token = access_token;
        this.expires_in = expires_in;
    }

    public Response_class() {
    }

    public String getToken_type() {
        return token_type;
    }

    public void setToken_type(String token_type) {
        this.token_type = token_type;
    }

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getExpires_in() {
        return expires_in;
    }

    public void setExpires_in(String expires_in) {
        this.expires_in = expires_in;
    }


}
Ballo Ibrahima
  • 461
  • 4
  • 10
0

You can use WebClient:

byte[] image = WebClient.create("locahost:80/image.jpg")
        .get()
        .accept(MediaType.IMAGE_JPEG)
        .retrieve()
        .bodyToMono(byte[].class)
        .block();
Woodchuck
  • 3,869
  • 2
  • 39
  • 70