Spring Rest API的返回列表

彼得·彭佐夫(Peter Penzov):

我想从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;
    }

如何映射结果?

安德鲁·托比尔科(Andrew Tobilko):

使用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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章