【问题标题】:Map Java DTO in response映射 Java DTO 作为响应
【发布时间】:2018-12-19 23:26:44
【问题描述】:

我有一个 Java 映射,我想使用 DTO 进行映射作为响应。我试过这个:

服务:

@Service
public class GatewaysService {

    public Map<Integer, String> getGatewaysList() {    
        Map<Integer, String> list = new HashMap<>();    
        list.put(1, "Bogus");    
        return list;
    }
}

API:

@Autowired
private GatewaysService gatewaysService;

    @GetMapping("gateways")
        public ResponseEntity<?> getGateways() {
            return ok(gatewaysService.getGatewaysList().map(mapper::toGatewayMap));
        }

DTO:

public class ContractGatewaysDTO {

    private Integer id;

    private String gateway;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getGateway() {
        return gateway;
    }

    public void setGateway(String gateway) {
        this.gateway = gateway;
    }
}

映射器:

Map<Integer, String> toGatewayMap(ContractGatewaysDTO dto);

但我得到错误:

The method map(ContractDTO) in the type ContractsMapper is not applicable for the arguments (mapper::toGatewayMap)

映射它的正确方法是什么?我想将 Java 映射转换为键值响应。

【问题讨论】:

  • Map什么时候有map方法?
  • 我想将 Java 映射转换为键值响应。 但映射正是键值。直接退货吧。
  • 我同意,但另一端 - angular 需要知道密钥。
  • 角度与它有什么关系。客户在这里无关紧要。

标签: java spring spring-boot spring-restcontroller spring-rest


【解决方案1】:

为什么不直接

@Autowired
private GatewaysService gatewaysService;

    @GetMapping("gateways")
        public Map<String,String> getGateways() {
            return gatewaysService.getGatewaysList();
        }

你不必在这里映射任何东西。

还有

除非在 Java 9+ 中有所改变,否则我不记得 java.uti.Map 有像您在这里使用的 map 方法

gatewaysService.getGatewaysList().map(mapper::toGatewayMap)

假设你试图做的是这样的:

List<Dto> yourlist= gatewaysService.getGatewaysList() //why MAP is named a LIST idk;
                        .entrySet() //can be any streamable collection - this comes with maps;
                        .stream()
                        .map(entry->new Dto(entry.key(),entry.value())
                        .collect(Collectors.toList());

【讨论】:

  • 如果我没记错的话,我会得到类似{1, Bogus }的东西。我需要获取内容{id: 1, gateway: Bogus }
  • 我从这个例子中得到了地图:@GetMapping("{id}") public ResponseEntity> get(@PathVariable String id) { return contractRepository .findById(Integer.parseInt(id)) 。 map(mapper::toDTO) .map(ResponseEntity::ok) .orElseGet(() -> notFound().build());
  • 我只能假设预期的输出。尽管如此,我还是添加了一个命题
  • 拥有可选项与拥有列表非常不同。
  • //why MAP is named a LIST idk; 是什么意思?
【解决方案2】:

我的想法是,您可能希望将 Map 转换为 DTO 实体,如果您需要这样做,您可以这样做:

return ResponseEntity.ok(getGatewaysList()
                                    .entrySet()
                                    .stream()
                                    .map( g -> new ContractGatewaysDTO(g.getKey(), g.getValue()))
                                    .collect(Collectors.toList()));

【讨论】:

    猜你喜欢
    • 2017-05-06
    • 2021-11-15
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    相关资源
    最近更新 更多