映射Java对象

时间:2018-11-02 21:56:36

标签: spring angular rest

我想创建一个服务以填充数据库中的下拉列表。我尝试过:

商人阶层:

export class Merchant {
  constructor(
    public id: string,
    public name: string,
    public state_raw: string,
    public users: string,
  ) {}
}

商家服务:

 getList(): Observable<Merchant> {
    return this.http.get<Merchant>(environment.api.urls.merchants.base, {});
  }

Rest Api展示:

@GetMapping
    public ResponseEntity<?> get() {
        return merchantRepository
                .findAll()
                .map(mapper::toDTO)
                .map(ResponseEntity::ok)
                .orElseGet(() -> notFound().build());
    }

SQL查询:

@Override
    public Iterable<Merchants> findAll() {
        String hql = "select e from " + Merchants.class.getName() + " e";
        TypedQuery<Merchants> query = entityManager.createQuery(hql, Merchants.class);
        List<Merchants> merchants = query.getResultList();
        return merchants;
    }

但是我得到这个错误:

The method map(mapper::toDTO) is undefined for the type Iterable<Merchants>

我应该如何正确实现响应的映射?

1 个答案:

答案 0 :(得分:0)

好像您要流式传输实体。

@GetMapping
public ResponseEntity<?> get() {
    return StreamSupport.stream(merchantRepository.findAll().spliterator(), false)
            .map(mapper::toDTO)
            .map(ResponseEntity::ok)
            .findFirst()
            .orElseGet(() -> notFound().build());
}