首页 > 解决方案 > 从rest api正确实施对象列表

问题描述

我想实现从 rest API 获取列表的 Angular 示例。我试过这个:

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;
    }

休息控制器:

@RestController
@RequestMapping("/merchants")
public class MerchantController {

    @GetMapping("/list")
    public Iterable<Merchants> getMerchantsList() {
        return merchantRepository
                .findAll();
    }
}

服务:

@Injectable({
  providedIn: 'root'
})
export class MerchantService {

  constructor(private http: HttpClient) {
  }    
  getList() {
    return this.http.get("...../api/merchants/list");
  }
}

零件:

@Component({
  selector: 'app-terminal',
  templateUrl: './terminal.component.html',
  styleUrls: ['./terminal.component.scss']
})
export class TerminalComponent implements OnInit {
  merchants: Merchant[];
  constructor(private merchantService: MerchantService,
              private router: Router,
              private route: ActivatedRoute) {
  }   

  ngOnInit() {               
    this.merchantService.getList();
  }    
}

但是当我通过网页锁定组件时,什么也没有发生。你能给我一些建议,我哪里错了吗?可能我的打字稿不正确?

标签: springangulartypescript

解决方案


您需要调用subscribeObservable否则它不会发出 HTTP 请求

ngOnInit() {               
  this.merchantService.getList().subscribe(res => {
    console.log("The response is", res); 
  });
}    

推荐阅读