当前位置: 首页 > news >正文

SpringBoot整合Angular应用第三弹-渲染RestAPI数据

上篇文章我们讲解了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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 <br/>
 * 描述 : UserController <br/>
 * 作者 : qianmoQ <br/>
 * 版本 : 1.0 <br/>
 * 创建时间 : 2018-03-13 下午2:29 <br/>
 * 联系作者 : <a href="mailTo:shichengoooo@163.com">qianmoQ</a>
 */
@RestController
@RequestMapping(value = {"user"})
public class UserController {
 
    /**
     * 获取用户集合
     *
     * @return 用户集合数据
     */
    @RequestMapping(value = "list")
    ConcurrentHashMap<String, String> getUserList() {
        ConcurrentHashMap<String, String> userMap = new ConcurrentHashMap<String, String>();
        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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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<any> {
    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);
  }
 
}
复制代码

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

相关文章:

  • CentOS从零开始部署Nodejs项目
  • d6
  • Jeff Bean谈Flink与流式处理的5大新发现
  • 解决 scapy “NameError: global name 'wrpcap' is not defined” 错误
  • 【xshell】xshell设置快捷键 设置Ctrl+C Ctrl+V快捷键为复制粘贴
  • Android FlatBuffers数据交互
  • 【man】 查看命令帮助文档
  • react 使用 react-loadable分包
  • Emacs学习(一)
  • 存档3
  • 笔记-SSZipArchive使用以及遇到的问题
  • Vue项目部署遇到的坑(你肯定会遇到!)
  • Hyper-v 与Oracle VM VirtualBox 之间的冲突问题
  • 手把手教您将 libreoffice 移植到函数计算平台
  • 通过一个实际例子理解Kubernetes里pod的自动scale - 水平自动伸缩
  • 【5+】跨webview多页面 触发事件(二)
  • 2017-09-12 前端日报
  • Date型的使用
  • HashMap ConcurrentHashMap
  • JS+CSS实现数字滚动
  • JS函数式编程 数组部分风格 ES6版
  • leetcode386. Lexicographical Numbers
  • Linux gpio口使用方法
  • Linux中的硬链接与软链接
  • Python中eval与exec的使用及区别
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • SpringBoot几种定时任务的实现方式
  • Spring核心 Bean的高级装配
  • Terraform入门 - 1. 安装Terraform
  • 成为一名优秀的Developer的书单
  • 对象管理器(defineProperty)学习笔记
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 后端_MYSQL
  • 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
  • 思考 CSS 架构
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 小而合理的前端理论:rscss和rsjs
  • 赢得Docker挑战最佳实践
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • NLPIR智能语义技术让大数据挖掘更简单
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • (12)Hive调优——count distinct去重优化
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (多级缓存)缓存同步
  • (二)丶RabbitMQ的六大核心
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (七)c52学习之旅-中断
  • (算法)前K大的和
  • (转) Face-Resources
  • (转)母版页和相对路径
  • (转载)Linux 多线程条件变量同步