spring - 从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();
}
}
但是当我通过网页锁定组件时,什么也没有发生。你能给我一些建议,我哪里错了吗?可能我的打字稿不正确?
解决方案
您需要调用subscribe
,Observable
否则它不会发出 HTTP 请求
ngOnInit() {
this.merchantService.getList().subscribe(res => {
console.log("The response is", res);
});
}
推荐阅读
- python - 来自一列的 Y 轴来自不同列的 x 到不同的子图中
- java - 从不同版本的 java 项目文件创建新的本地 git 存储库
- html - 下划线css中的前5个元素
- node.js - 使用 next.js,为什么我突然开始看到 Module not found: Error: Can't resolve 'fs'?
- node.js - 在 Node.js 模型中使用 i18next
- javascript - 有没有一种方法可以在制表器 js 上按“月份”对数据进行分组,同时拥有完整的日期?
- html - 如何在另一个 div 上方添加隐藏的 div?
- javascript - 带有路由器的 Vue 模态
- java - 如何在 Java 中删除(有效)最终数组
- python - 将base64编码的bmp图像转换为python后端的像素值