Java 来自springrestapi的返回列表

Java 来自springrestapi的返回列表,java,spring,spring-boot,response,httpresponse,Java,Spring,Spring Boot,Response,Httpresponse,我想从Spring rest api返回列表: @GetMapping("merchant_country") public ResponseEntity<?> getMerchantCountry() { return ok(Contracts.getSerialversionuid()); } @GetMapping(“商户国家”) 公共响应getMerchantCountry(){ 返回ok(Contracts.getSerialversion

我想从Spring rest api返回列表:

@GetMapping("merchant_country")
    public ResponseEntity<?> getMerchantCountry() {
        return ok(Contracts.getSerialversionuid());
    }
@GetMapping(“商户国家”)
公共响应getMerchantCountry(){
返回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;
    }
私有映射getCountryNameCodeList(){
字符串[]countryCodes=Locale.getISOCountries();
映射列表=新的HashMap();
for(字符串countryCode:countryCodes){
语言环境obj=新语言环境(“,国家代码);
put(obj.getDisplayCountry().toString(),obj.getCountry());
}
退货清单;
}

如何映射结果?

当您需要对HTTP响应进行更多控制时(例如设置HTTP头,提供不同的状态代码),请使用
ResponseEntity

在其他情况下,您可以简单地返回一个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() { ... }

}
类控制器{
@自动连线
私人服务;
@GetMapping(“商户/国家”)
公共地图getMerchantCountry(){
return service.getCountryNameCodeList();
}
}
班级服务{
公共映射getCountryNameCodeList(){…}
}

Locate#getCountry
返回
String
,因此它可能是
Map
希望这有帮助,您必须按照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);
    }
类控制器{
@自动连线
私人服务;
@GetMapping(“商户/国家”)
公共响应getMerchantCountry(){
return service.getCountryNameCodeList();
}`在这里输入代码`
}
班级服务
{
private ResponseEntity getCountryNameCodeList(){
字符串[]countryCodes=Locale.getISOCountries();
映射列表=新的HashMap();
for(字符串countryCode:countryCodes){
语言环境obj=新语言环境(“,国家代码);
put(obj.getDisplayCountry().toString(),obj.getCountry());
}
返回新的响应属性(列表,HttpStatus.OK);
}

您想返回map或list?这两种方法之间没有关系?好的,当我从angular code调用GET merchant\u country时,如何将getCountryNameCodeList返回为map?您不能将列表设置为map的值吗?这是开始@PeterPenzov的好地方