Retrofit: Could not locate ResponseBody converter for

0

I'm working on a system and I need Retrofit2 (with Spring Boot) but I'm not getting it.

The source and error follow.

@JsonIgnoreProperties({"codibge", "codestado"}) 
public class CEP {

private String cep;
private String logradouro;
private String complemento;
private String bairro;
private String cidade;
private String estado;
public interface HelloService {
    @GET("{cep}")
    Call<CEP> getEndereco(@Path("cep") String cep);
}
public class HelloController {

private HelloService helloService;
private static final Logger logger = LoggerFactory.getLogger(App.class);

public HelloController() {
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://api.postmon.com.br/v1/cep/")
        .build();       
    helloService = retrofit.create(HelloService.class);
}

public void getEndereco(String cep) {
    Response<CEP> response = null;
    try {
        response = helloService.getEndereco(cep).execute();
        if (response.isSuccessful()) {
            logger.info("Sucesso...");
        } else {
            logger.error("Erro...");
        }
    } catch (IOException e) {
        logger.error(e.toString());
    }
}
@SpringBootApplication
public class App {

    private static final Logger log = LoggerFactory.getLogger(App.class);

    public static void main(String args[]) {
        SpringApplication.run(App.class);
        HelloController hello = new HelloController();
        hello.getEndereco("82560435");
    }
}

When testo returns the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Unable to create converter for class br.com.hello.api.CEP
for method HelloService.getEndereco
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:755)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:741)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:172)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at com.sun.proxy.$Proxy72.getEndereco(Unknown Source)
at br.com.hello.api.HelloController.getEndereco(HelloController.java:29)
at br.com.hello.api.App.main(App.java:17)
Caused by: java.lang.IllegalArgumentException: Could not locate ResponseBody converter for class br.com.hello.api.CEP.
Tried:
* retrofit2.BuiltInConverters
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:351)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:313)
at  retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:739)
... 6 more

What am I doing wrong?

    
asked by anonymous 16.04.2018 / 04:05

1 answer

3

Retrofit by itself does not do JSON serialization. He delegates this to converters. So when building the Retrofit object, you must pass the converter of your preference. They support Jackson, which is the default SpringBoot:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://api.postmon.com.br/v1/cep/")
    .addConverterFactory(JacksonConverterFactory.create())
    .build();

You can also pass an object mapper if you want to share the SpringBoot settings:

.addConverterFactory(JacksonConverterFactory.create(myObjectMapper))

Remembering that you need to add the dependency:

<dependency>
  <groupId>com.squareup.retrofit2</groupId>
  <artifactId>converter-jackson</artifactId>
  <version>2.4.0/version>
</dependency>
    
16.04.2018 / 12:55