0

I have this code :

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Application {

    public static void main(String args[]) {
        SpringApplication.run(Application.class);

    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {


        RestTemplate restTemplate =  builder.rootUri("http://login.xxx.com/").basicAuthorization("user", "pass").build();
        return restTemplate;
    }

    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception {



        return args -> {
             restTemplate.getForObject(
                    "http://login.xxx.com/ws/YY/{id}", YY.class,
                    "123");

        };

    }

}

but I'm getting this error : Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.xxx.test.YY] and content type [application/xml;charset=ISO-8859-1]

How can I add MediaType.APPLICATION_JSON to the header and add the header to the restTemplate and do getForObject ?

user7916020
  • 109
  • 6
  • 14

1 Answers1

0

You don't need to add the accept header when using any of the get methods, RestTemplate will do that automatically. If you look inside RestTemplate's constructor, you can see that it automatically checks the classpath and add common message converters. So you may need to check you're classpath (or step into the constructor to see which converters it autodetects.

If you need to add custom headers, like Bearer authentication, you can always use the exchange method to build the request exactly as you like. Here is an example that should work, I have added the Jackson message converter explicetly, so you should get an compilation error if it is not in your classpath.

import java.net.URI;
import java.util.Map;

import com.google.common.collect.Lists;
import org.springframework.http.*;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class Main {

    public static void main(String[] args) throws Exception {

        RestTemplate template = new RestTemplate();
        template.setMessageConverters(Lists.newArrayList(new MappingJackson2HttpMessageConverter()));
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON));
        ResponseEntity<Map> forEntity = template.exchange(new RequestEntity<>(null, headers, HttpMethod.GET, new URI("https://docs.tradable.com/v1/accounts")), Map.class);
        System.out.println("forEntity.getBody() = " + forEntity.getBody());
    }
}
Klaus Groenbaek
  • 4,820
  • 2
  • 15
  • 30