Spring: how Jackson can format a composite Json response into objects

Asked

Viewed 28 times

-1

Well, I’m doing a Spring project along with Spring-cloud-openFeign for integration with a Fipe api to access car values. See the class:

@Service
@FeignClient(name = "FipeService", url = "https://parallelum.com.br/fipe/api/v1")
public interface FipeService {
    
    @GetMapping("/{type}/marcas")
    public List<FipeResponse> getBrands(@PathVariable String type);

    @GetMapping("/{type}/marcas/{brandCode}/modelos")
    public List<FipeResponse> getModels(@PathVariable String type, @PathVariable String brandCode);

    @GetMapping("/{type}/marcas/{brandCode}/modelos/{modelCode}/anos")
    public List<FipeResponse> getYears(@PathVariable String type, @PathVariable String brandCode, @PathVariable String modelCode);

    @GetMapping("/{type}/marcas/{brandCode}/modelos/{modelCode}/anos/{year}")
    public FipeResponse getPrice(@PathVariable String type, @PathVariable String brandCode, @PathVariable String modelCode, @PathVariable String year);
}

Fiperesponse is a class of mine used by Jackson to insert the return of the api into it, which he does in the method getBrands() because the Json answer is simple, an array only with name and code. See the class:

public class FipeResponse {

private String nome;
private String codigo;
private String valor;

public FipeResponse(String nome, String codigo, String valor) {
    this.nome = nome;
    this.codigo = codigo;
    this.valor = valor;
}

public FipeResponse(){

}

getters e setters

}

However, in the second endpoint I have a problem, the getModels returns an array of models and years (test the link: https://parallelum.com.br/fipe/api/v1/carros/marcas/59/modelos) and Jackson can not insert in Fiperesponse obviously, but, I tried inside Fiperesponse to add two other classes as composition, one for models and the other for years, both with name and attribute code only, and I inserted in Fiperesponse so:

public class FipeResponse {

private String nome;
private String codigo;
private String valor;
private List<FipeAnos> anos;
private List<FipeModelos> modelos;

public FipeResponse(String nome, String codigo, String valor, List<FipeAnos> anos, List<FipeModelos> modelos) {
    this.nome = nome;
    this.codigo = codigo;
    this.valor = valor;
    this.anos = anos;
    this.modelos = modelos;
}

public FipeResponse(){

}

getters e setters

}

And yet Jackson still can’t enter the data using endpoint getModels(), but works normally using getBrands(), I can access the attributes and everything. What I did wrong?

  • I don’t understand the downvote on the question... If further details are required just ask, now give -1 and not say anything does not solve at all.

1 answer

0

Getbrands : The request to this endpoint actually returns a list:

[
  {
    "nome": "Acura",
    "codigo": "1"
  },
  {
    "nome": "Agrale",
    "codigo": "2"
  }...
]

endpoint getModels returns an object, and this object contains a list of templates :

{
  "modelos": [
    {
      "nome": "AMAROK CD2.0 16V/S CD2.0 16V TDI 4x2 Die",
      "codigo": 5585
    },
    {
      "nome": "AMAROK CD2.0 16V/S CD2.0 16V TDI 4x4 Die",
      "codigo": 5586
    },
    ....
}

To get the answer of this endpoint you must model an object that represents this json, for example:

public class FipeModelo {
   public String modelo;
   public Integer codigo;
}

Public class FipeModelos {
   public List<FipeModelo> modelos;
}

pay attention that the Fipetemplates receives a Fipetemplate List and the variable is called models, as the answer that comes in the endpoint you passed.

And in feign’s service class it would be:

@GetMapping("/{type}/marcas/{brandCode}/modelos")
public FipeModelos getModels(@PathVariable String type, @PathVariable String brandCode);

Browser other questions tagged

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