Java 从SpringRESTAPI返回Hashmap

Java 从SpringRESTAPI返回Hashmap,java,spring,spring-boot,Java,Spring,Spring Boot,我想使用此代码从Rest API获取一些数据: public Map<Integer, String> getCategoriesList() { Map<Integer, String> list = new HashMap<>(); list.put(1, "Electronics"); list.put(2, "Outdoor and Sports");

我想使用此代码从Rest API获取一些数据:

public Map<Integer, String> getCategoriesList() {

        Map<Integer, String> list = new HashMap<>();
        list.put(1, "Electronics");
        list.put(2, "Outdoor and Sports");
        list.put(3, "Home and Garden");
        list.put(4, "Home appliances");
        list.put(5, "Air conditioners and heaters");
        list.put(6, "IT accessories");
        list.put(7, "Photo and Video");
        list.put(8, "TV Video and Gaming");

        return list;
    }

@GetMapping("categories")
public ResponseEntity<List<String>> getCategoriesList() {

    return (ResponseEntity<List<String>>) categoriesService.getCategoriesList();
}
publicmap getCategoriesList(){
映射列表=新的HashMap();
列表。put(1,“电子设备”);
列表。put(2,“户外运动”);
列表。放置(3,“家与花园”);
列表.put(4,“家用电器”);
清单.put(5,“空调和加热器”);
列表。放置(6,“IT配件”);
列表。放置(7,“照片和视频”);
列表.put(8,“电视视频和游戏”);
退货清单;
}
@GetMapping(“类别”)
公共响应属性getCategoriesList(){
return(ResponseEntity)categoriesService.getCategoriesList();
}
我得到一个错误:
class java.util.HashMap不能强制转换到class org.springframework.http.ResponseEntity


将此数据作为响应返回的适当方式是什么?

您不能像这样将一种类型转换为另一种类型……试试这个

响应类型:

@GetMapping("categories")
public ResponseEntity<Map<Integer, String>> getCategoriesList() {
    return new ResponseEntity<Map<Integer,String>>(categoriesService.getCategoriesList(), HttpStatus.OK);
}
@GetMapping(“类别”)
公共响应属性getCategoriesList(){
返回新的ResponseEntity(categoriesService.getCategoriesList(),HttpStatus.OK);
}
无响应包装

@GetMapping("categories")
@ResponseStatus(code = HttpStatus.OK)
public Map<Integer, String> getCategoriesList() {
    return categoriesService.getCategoriesList();
}
@GetMapping(“类别”)
@ResponseStatus(代码=HttpStatus.OK)
公共地图getCategoriesList(){
返回categoriesService.getCategoriesList();
}
由于这两种类型的映射对jackson来说都是已知的(我想这就是您在spring中用于序列化/反序列化的映射),因此不需要做更多的事情

参考:

您不能像这样将一种类型转换为另一种类型……试试这个

响应类型:

@GetMapping("categories")
public ResponseEntity<Map<Integer, String>> getCategoriesList() {
    return new ResponseEntity<Map<Integer,String>>(categoriesService.getCategoriesList(), HttpStatus.OK);
}
@GetMapping(“类别”)
公共响应属性getCategoriesList(){
返回新的ResponseEntity(categoriesService.getCategoriesList(),HttpStatus.OK);
}
无响应包装

@GetMapping("categories")
@ResponseStatus(code = HttpStatus.OK)
public Map<Integer, String> getCategoriesList() {
    return categoriesService.getCategoriesList();
}
@GetMapping(“类别”)
@ResponseStatus(代码=HttpStatus.OK)
公共地图getCategoriesList(){
返回categoriesService.getCategoriesList();
}
由于这两种类型的映射对jackson来说都是已知的(我想这就是您在spring中用于序列化/反序列化的映射),因此不需要做更多的事情

参考:

我无法推断。。。好吧,你需要在这个场景中扮演角色,但是你想实现什么呢?您想返回hashmap(根据问题)还是根据代码返回列表?我想返回hashmap-键-值对。但是…您可以从方法返回映射-您不需要使用ResponseEntity类型-我已经编辑了我的答案使用ResponseEntity包装器是一种好的做法吗?有什么好处?我无法推断。。。好吧,你需要在这个场景中扮演角色,但是你想实现什么呢?您想返回hashmap(根据问题)还是根据代码返回列表?我想返回hashmap-键-值对。但是…您可以从方法返回映射-您不需要使用ResponseEntity类型-我已经编辑了我的答案使用ResponseEntity包装器是一种好的做法吗?有什么好处?