我想从rest api 获取时区作为列表。我试过这个:
@GetMapping("timezone")
public ResponseEntity<?> getTimezone() {
return ResponseEntity.ok(timezoneService.getTimezoneList().entrySet().stream()
.map(g -> new AcquirerTimezoneDTO(g.getKey())).collect(Collectors.toList()));
}
DTO:
public class AcquirerTimezoneDTO {
private String timezone;
public AcquirerTimezoneDTO(String timezone) {
this.timezone = timezone;
}
public String getTimezone() {
return timezone;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
}
服务:
@Service
public class TimezoneService {
public List<String> getTimezoneList() {
List<String> list = new ArrayList<>();
String[] allTimeZones = TimeZone.getAvailableIDs();
Arrays.sort(allTimeZones);
for (int i = 0; i < allTimeZones.length; i++) {
list.add(allTimeZones[i]);
}
return list;
}
}
但是我收到 .entrySet() 错误
方法entrySet() 对于类型List 未定义
你知道我该如何解决这个问题吗?
最佳答案
entrySet()
是 Maps
拥有的方法,而不是 Lists
拥有的方法。
直接在列表上使用stream()
方法,无需entrySet()
,如下所示:
return ResponseEntity.ok(timezoneService.getTimezoneList().stream()
.map(g -> new AcquirerTimezoneDTO(g.getKey())).collect(Collectors.toList()));
关于java - 从rest api获取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54041089/