Вернуться список из весеннего отдыха API - PullRequest
0 голосов
/ 16 декабря 2018

Я хочу вернуть список из Spring rest api:

@GetMapping("merchant_country")
    public ResponseEntity<?> getMerchantCountry() {
        return ok(Contracts.getSerialversionuid());
    }

Я хочу получить содержимое здесь:

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;
    }

Как я могу отобразить результат?

Ответы [ 2 ]

0 голосов
/ 16 декабря 2018

Надеюсь, это поможет, вам нужно создать ResponseEntity, как предложено Andrew

class controller{
@Autowired
    private Service service;
@GetMapping("merchant_country")
    public ResponseEntity<?> getMerchantCountry() {
        return service.getCountryNameCodeList();
    }`enter code here`
}
class service
{
private ResponseEntity<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 new ResponseEntity<>(list, HttpStatus.OK);
    }
0 голосов
/ 16 декабря 2018

Используйте ResponseEntity, когда вам нужно больше контроля над ответом HTTP (например, установка заголовков HTTP, предоставление другого кода состояния).

В других случаях вы можете просто вернуть POJO (или коллекцию), и Spring будет обрабатывать все остальное за вас.

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 возвращает String, поэтомуможет быть Map<String, String>.

...