Api编写

1>     Gin框架的Api返回的数据格式有json,xml,yaml这三种格式。其中yaml这种格式是一种特殊的数据格式。(本人暂时没有实现获取节点值得操作)

2>     在apis文件夹下,新建一个data.go文件,作为获取api数据的业务逻辑代码.具体代码如下:

package apis

import (
"net/http"
"github.com/gin-gonic/gin"
. "GinLearn/GinLearn/models"
)
//Api调用的页面
func GetApiHtml(c *gin.Context){
c.HTML(http.StatusOK,"api.html",gin.H{
"title":"Go-Gin Api调用页面",
})
}
//Json格式的数据
func GetJsonData(c *gin.Context) {
//得到请求的参数
search:=c.PostForm("search")
//得到用户的数据
datalist:=GetPersonList(1,10,search)
//得到记录的总数
count:=GetRecordNum(search)
//返回结果
c.JSON(http.StatusOK, gin.H{
"datalist": datalist,
"count":count,
"pagesize":3,
"pageno":1,
})
} //Xml格式的数据
func GetXmlData(c *gin.Context) {
//得到请求的参数
search:=c.PostForm("search")
//得到用户的数据
datalist:=GetPersonList(1,10,search)
//得到记录的总数
count:=GetRecordNum(search)
//返回结果
c.XML(http.StatusOK, gin.H{
"datalist": datalist,
"count":count,
"pagesize":3,
"pageno":1,
})
} //Xml格式的数据
func GetYamlData(c *gin.Context) {
//得到请求的参数
search:=c.PostForm("search")
//得到用户的数据
datalist:=GetPersonList(1,10,search)
//得到记录的总数
count:=GetRecordNum(search)
//返回结果
c.YAML(http.StatusOK, gin.H{
"datalist": datalist,
"count":count,
"pagesize":3,
"pageno":1,
})
} //Json格式的数据
func GetParamsJsonData(c *gin.Context) {
//得到请求的参数
search:=c.PostForm("search")
//得到用户的数据
datalist:=GetPersonList(1,10,search)
//得到记录的总数
count:=GetRecordNum(search)
//返回结果
c.JSON(http.StatusOK, gin.H{
"datalist": datalist,
"count":count,
"pagesize":3,
"pageno":1,
"search":search,
})
}

  

3>     在views文件夹下新建一个api.html页面作为测试获取api数据的展示页面.具体代码如下:

<!DOCTYPE html>
 
<html>
      <head>
        <title>{{.title}}</title>
        <link rel="shortcut icon" href="/static/img/favicon.png" />
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css"/>
<script type="text/javascript" src="/static/js/jquery-2.1.1.min.js"></script> 
<script type="text/javascript" src="/static/bootstrap/js/bootstrap.min.js"></script> 
      </head>    
    <body>
<div class="container">
<!--请求得到字典数据-->
<div style="width:100%;height:50px;">
<input type="text" id="search" placeholder="请输入参数"/>
<button onclick="getparams()" class="btn btn-primary">得到参数</button>
<label id="txtparams"></label>
</div>
<!--请求得到Json数据-->
<div style="width:100%;height:50px;">
<button onclick="getjson()" class="btn btn-primary">得到Json</button>
<label id="txtjson"></label>
</div>
<!--请求得到Xml数据-->
<div style="width:100%;height:50px;">
<button onclick="getxml()" class="btn btn-primary">得到Xml</button>
<label id="txtxml"></label>
</div>
<!--请求得到Yaml数据-->
<div style="width:100%;height:50px;">
<button onclick="getYaml()" class="btn btn-primary">得到Yaml</button>
<label id="txtyaml"></label>
</div>
</div> <!--JS部分-->
<script type="text/javascript">
//得到参数
function getparams(){
$.ajax({
type:'get',
url:'/api/paramsdata',//此处的是json数据的格式
data:{
search:$("#search").val()
},
success:function(result){
console.log('获取参数的数据')
console.log(result)
$("#txtparams").html("记录总数:"+result.count
+",记录I:"+result.datalist[0].first_name+result.datalist[0].last_name+",记录II:"
+result.datalist[1].first_name+result.datalist[1].last_name+"...");
}
})
}
//得到Json
function getjson(){
$.ajax({
type:'get',
url:'/api/jsondata',
dataType:'json',//此处的是json数据的格式
data:{
search:$("#search").val()
},
success:function(result){
console.log('获取json的数据')
console.log(result)
$("#txtjson").html("json的结果:"+result.count
+",记录1:"+result.datalist[0].first_name+result.datalist[0].last_name+",记录2:"
+result.datalist[1].first_name+result.datalist[1].last_name+"...");
}
})
}
//得到Xml
function getxml(){
$.ajax({
type:'get',
url:'/api/xmldata',
dataType:'xml',//此处的是xml数据的格式
data:{
search:$("#search").val()
},
success:function(result){
console.log('获取xml的数据')
console.log(result) $("#txtxml").html("xml的结果:"+$(result).text());
}
})
}
//得到yaml
function getYaml(){
$.ajax({
type:'get',
url:'/api/yamldata',
data:{
search:$("#search").val()
},
success:function(result){
console.log('获取yaml的数据')
console.log(result)
$("#txtyaml").html("yaml的结果:"+result);
}
})
} </script>
    </body>
</html>

  

4>     在路由器文件router.go中添加api部分的路由。具体代码如下:

package routers

import (
"github.com/gin-gonic/gin"
. "GinLearn/GinLearn/apis" //api部分
. "GinLearn/GinLearn/controllers" //constroller部分
) func InitRouter() *gin.Engine{
router := gin.Default()
//Hello World
router.GET("/", IndexApi)
//渲染html页面
router.LoadHTMLGlob("views/*")
router.GET("/home/index", ShowHtmlPage)
//列表页面
router.GET("/home/list", ListHtml)
router.POST("/home/PageData", GetDataList)
router.POST("/home/PageNextData", PageNextData) //新增页面
router.GET("/home/add", AddHtml)
router.POST("/home/saveadd", AddPersonApi) //编辑页面
router.GET("/home/edit", EditHtml)
router.POST("/home/saveedit", EditPersonApi) //删除
router.POST("/home/delete", DeletePersonApi) //Bootstrap布局页面
router.GET("/home/bootstrap", Bootstraphtml) //文件的上传和下载
router.GET("/home/fileopt", Fileopthtml)
router.POST("/home/fileuplaod", Fileupload)
router.GET("/home/filedown", Filedown) //文件的创建删除和读写
router.GET("/home/filerw", Filerwhtml)
router.POST("/home/addfile", FilerCreate)//创建文件
router.POST("/home/writefile", FilerWrite)//写入文件
router.POST("/home/readfile", FilerRead)//读取文件
router.POST("/home/deletefile", FilerDelete)//删除文件 //api调用的部分
router.GET("/home/api", GetApiHtml)
router.GET("/api/jsondata", GetJsonData)
router.GET("/api/xmldata", GetXmlData)
router.GET("/api/yamldata", GetYamlData)
router.GET("/api/paramsdata", GetParamsJsonData)
return router
}

  

5>     编译测试,具体效果如下:

6>     下一章讲布局页面

Gin-Go学习笔记六:Gin-Web框架 Api的编写的更多相关文章

  1. python 学习笔记十五 web框架

    python Web程序 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. Python的WEB框架分为两类: 自己写socket,自 ...

  2. tornado 学习笔记9 Tornado web 框架---模板(template)功能分析

            Tornado模板系统是将模板编译成Python代码.         最基本的使用方式: t = template.Template("<html>{{ myv ...

  3. # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)

    目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...

  4. Go语言笔记[实现一个Web框架实战]——EzWeb框架(一)

    Go语言笔记[实现一个Web框架实战]--EzWeb框架(一) 一.Golang中的net/http标准库如何处理一个请求 func main() { http.HandleFunc("/& ...

  5. ASP.NET MVC Web API 学习笔记---第一个Web API程序

    http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...

  6. Typescript 学习笔记六:接口

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  7. Spring实战第八章学习笔记————使用Spring Web Flow

    Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...

  8. Spring实战第五章学习笔记————构建Spring Web应用程序

    Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...

  9. java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)

    java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...

随机推荐

  1. 201871010126 王亚涛《面向对象程序设计(Java)》第十二周学习总结

      内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/ ...

  2. java中判断两个对象是否相等

    package ceshi.com.job; import java.util.ArrayList; import java.util.Arrays; import java.util.List; p ...

  3. day7_7.5 字符表与文件操作

    一.字符编码. 1. 在计算机的运行中,有三大部分组成,计算机硬件,操作系统,应用程序,当使用者在使用文档编写文字时,所显示的所有字符都存储在内存中,随后再存储在硬盘里,(仅限文字),所以,当美国人发 ...

  4. shell的使用技巧

    推荐使用的远程连接软件以及vi编辑器的基本使用 简介:远程连接软件 与 vi命令的基本使用 (1)软件: CRT 已经下载好的压缩包 直接双击 点击新建会话  点击下一步  输入主机名  下一步    ...

  5. Mybatis ResultMap多表映射DTO

    解决问题:n+1问题,之前我的习惯是拿到单表里面的数据,然后遍历,再拿到一个与其他表对应的逻辑上的外键,然后for循环去查找其他表的数据(原因是数据量小,没有在意,还有主要是不想建外键,你知道的,外键 ...

  6. SLAM:

    十四讲: 传感器约束了外部环境 测到的通常都是一些间接的物理量而不是直接的位置数据 只能通过一些间接的手段,从这些数据推算自己的位置 好处是没有对环境提出任何要求 camera:单目.双目.深度 Mo ...

  7. 启动hadoop后jps没有namenode,并且启动报错9000

    启动hadoop报错: 解决方法: 我发现没有9000端口被占用,也不知道9000到哪去了,但是也没有NameNode,于是,直接把NameNode格式化了,再重启HDFS即可. 格式化命令:

  8. Cookie 技术

    Cookie 学习: 问题: HTTP 协议是没有记忆功能的,一次请求结束后,相关数据会被销毁.如果第二次的请求需要使用相同的请求数据怎么办呢?难道是让用户再次请求书写吗? 解决:使用 Cookie ...

  9. yum源加速,替换为阿里云镜像

    问题 使用yum命令安装mysql时,发现下载速度很慢,于是决定换成阿里的yum源 解决方法 参考自:https://www.jianshu.com/p/b7cd2f9fb8b7 首先备份一下原先的y ...

  10. cnpm包管理

    C:\Users\Administrator>npm config set registry http://registry.npm.taobao.org vue 安装 npm install ...