0

I have this json response back from API:

With result:

{
  "Result": {"name":"Jason"},
  "Status": 1
}

Without result:

{
  "Result": "Not found",
  "Status": 1
}

As you can see the format the different.

When using Feign when not found I will certainly hit the conversion error:

@GetMapping(path = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseDto sendRequest(@RequestParam("id") String ID);
}

Any way to resolve this without changing the API? I will certainly hit with an error

Cannot construct instance of `com.clients.dto.ResponseDto` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Not found.')
Alvin
  • 8,219
  • 25
  • 96
  • 177

1 Answers1

0

You can use generics to solve the issue.

public class ResponseDto<T>{

private T Result;
private Long Status;

...getters and setters....

}

maybe create model for another class as well:

public class SuccessResult{

private String name;

...getters and setters....

}

Now in Feign:

@GetMapping(path = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
    public <T> ResponseDto<T> sendRequest(@RequestParam("id") String ID);
}

OR....

@GetMapping(path = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseDto<?> sendRequest(@RequestParam("id") String ID);
}

After you get the response use object mapper to convert the value:

import com.fasterxml.jackson.databind.ObjectMapper;

.....

@Autowired
ObjectMapper objectMapper;

//....your method and logic...

ResponseDto responseDto = sendRequest("1");

String res = null;
String successResponse = null;


if(responseDto.getResult() instanceof String)
{
    res = objectMapper.convert(responseDto.getResult(), String.class);

} else{

    successResponse = objectMapper.convert(responseDto.getResult(), SuccessResult.class);

}
Vee Dee
  • 133
  • 1
  • 7
  • Two corrections in the last code part, ---- ResponseDto> responseDto = sendRequest("1"); -/instead of/- ResponseDto responseDto = sendRequest("1"); === and === SuccessResult successResponse = null; -/ instead of /- String successResponse = null; ---in the last code part. – Vee Dee Feb 22 '23 at 07:04