Retrofit: Could not locate Responsebody converter for

Asked

Viewed 462 times

0

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

Follow the source and error.

@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?

  • 1

    Retrofit treats the answers as an entity, a converter is required, you can use Gson or Jacksonjson, for each library you do your due mapping

  • In this example of the zip code worked, I will try now with the FCM I believe it will also work. Thank you

1 answer

4


Retrofit itself does not [de]serialize JSON. It delegates this to converters. So, when building the Retrofit object, you must pass the convert of your preference. They support Jackson, which is the standard of Springboot:

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

You can also pass a mapper Object if you want to share Springboot settings:

.addConverterFactory(JacksonConverterFactory.create(myObjectMapper))

Remembering that you will need to add the dependency:

<dependency>
  <groupId>com.squareup.retrofit2</groupId>
  <artifactId>converter-jackson</artifactId>
  <version>2.4.0/version>
</dependency>
  • Perfect... It worked out. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.