I want to return List from Spring rest api:
@GetMapping("merchant_country")
public ResponseEntity<?> getMerchantCountry() {
return ok(Contracts.getSerialversionuid());
}
I want to get the content from here:
private Map<String, Object> getCountryNameCodeList() {
String[] countryCodes = Locale.getISOCountries();
Map<String, Object> list = new HashMap<>();
for (String countryCode : countryCodes) {
Locale obj = new Locale("", countryCode);
list.put(obj.getDisplayCountry().toString(), obj.getCountry());
}
return list;
}
How I can map the result?
Use ResponseEntity when you need more control over the HTTP response (e.g. setting HTTP headers, providing a different status code).
In other cases, you can simply return a POJO (or a collection), and Spring will handle everything else for you.
class Controller {
@Autowired
private Service service;
@GetMapping("merchant_country")
public Map<String, Object> getMerchantCountry() {
return service.getCountryNameCodeList();
}
}
class Service {
public Map<String, Object> getCountryNameCodeList() { ... }
}
Locate#getCountry returns String, so it could be Map<String, String>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With