Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return list from Spring rest api

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?

like image 694
Peter Penzov Avatar asked Jan 25 '26 11:01

Peter Penzov


1 Answers

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>.

like image 57
Andrew Tobilko Avatar answered Jan 28 '26 01:01

Andrew Tobilko