博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot整合Angular应用第三弹-渲染RestAPI数据
阅读量:7231 次
发布时间:2019-06-29

本文共 3846 字,大约阅读时间需要 12 分钟。

上篇文章我们讲解了Angular去和SpringBoot进行整合操作.这篇文章我们主要讲解使用整合后的Angular去解析SpringBoot返回的RestAPI数据到页面.

  • 创建UserController数据接口用于生产模拟测试数据
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * 

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.edurt; import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.ConcurrentHashMap; /** * UserController

* 描述 : UserController
* 作者 : qianmoQ
* 版本 : 1.0
* 创建时间 : 2018-03-13 下午2:29
* 联系作者 : qianmoQ */@RestController@RequestMapping(value = {
"user"})public class UserController { /** * 获取用户集合 * * @return 用户集合数据 */ @RequestMapping(value = "list") ConcurrentHashMap
getUserList() { ConcurrentHashMap
userMap = new ConcurrentHashMap
(); userMap.put("user1", "user1"); userMap.put("user2", "user2"); userMap.put("user3", "user3"); userMap.put("user4", "user4"); return userMap; } }复制代码

  • 添加angular后台数据交互服务AppService文件
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * 

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import {Injectable} from '@angular/core';import {Http} from '@angular/http';import 'rxjs/add/operator/catch';import 'rxjs/add/operator/map'; /** * 用户服务 */@Injectable()export class AppService { constructor(private http: Http) { } getUserList(): Promise

{ const url = '/user/list'; return this.http.get(url) .toPromise() .then(response => response.json()) .catch(this.handleError); } private handleError(error: Response | any) { let errMsg: string; console.error(errMsg); return Promise.reject(errMsg); } }复制代码

  • 修改appmodule引入数据服务和httpmodule
import {AppService} from "./app.service";import {HttpModule} from "@angular/http"; @NgModule({  declarations: [    ...  ],  imports: [    ..    HttpModule  ],  providers: [    AppService  ],  bootstrap: [AppComponent]})export class AppModule {}复制代码
  • 修改app组件文件进行数据渲染
import {Component} from '@angular/core';import {AppService} from "./app.service"; @Component({  selector: 'app-root',  templateUrl: './app.component.html',  styleUrls: ['./app.component.css']})export class AppComponent {  title = 'app';   public userList;   constructor(private appService: AppService) {    this.userList = this.appService.getUserList();    console.log(this.userList);  } }复制代码

由于我们直接打印到了控制台所以运行服务打开浏览器查看控制台即可看到从后台获取的数据.

转载地址:http://nzcfm.baihongyu.com/

你可能感兴趣的文章
spring cloud构建互联网分布式微服务云平台-SpringCloud集成项目简介
查看>>
基于房源的画像分析
查看>>
80% UI 初学者走过的弯路,你走了几条?
查看>>
文档和元素的几何滚动
查看>>
php 设计模式
查看>>
Java springcloud B2B2C o2o多用户商城 springcloud架构(八)springboot整合mongodb
查看>>
3年工作经验的Java程序员面试经过
查看>>
Mysql 批量写入数据,对于这类性能问题,你是如何优化的
查看>>
MySQL无法启动几种常见问题小结
查看>>
阿里CTO:阿里所有技术和产品输出都将必须通过阿里云进行
查看>>
更好用的集群限流功能,Sentinel 发布 v1.4.2
查看>>
Python(生成执行文件)
查看>>
redis安装配置 - ttlsa教程系列之redis
查看>>
Linux --DHCP服务器配置;DHCP服务器中继
查看>>
IE版本多的可爱_已迁移
查看>>
eclipse查看jar包中class的中文注释乱码问题的解决
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
mariadb安装
查看>>
vue+vuex+axios+echarts画一个动态更新的中国地图
查看>>