[英]Return list from Spring rest api
I want to return List from Spring rest api: 我想从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 (eg setting HTTP headers, providing a different status code). 当您需要对HTTP响应进行更多控制时(例如,设置HTTP标头,提供不同的状态代码),请使用ResponseEntity
。
In other cases, you can simply return a POJO (or a collection), and Spring will handle everything else for you. 在其他情况下,您只需返回一个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
returns String
, so it could be Map<String, String>
. Locate#getCountry
返回String
,所以它可能是Map<String, String>
。
Hope this helps, you have to create ResponseEntity as suggested by Andrew 希望这会有所帮助,您必须按照Andrew的建议创建ResponseEntity
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.