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. 201777010217-金云馨《面向对象程序设计(java)》第十六周学习总结

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

  2. ReportMachine打印条形码的问题

    ReportMachine打印条形码的问题 最近用RM报表来打印条形码,调试过程非常顺利,扫描枪识别也很正常,唯独斑马打印机的走纸不准确是个问题,正好客户不想用这种纸型,并定制了新纸型,心想等新纸型到 ...

  3. java修饰符的总结

    引言:Java的修饰符根据修饰的对象不同,分为类修饰符.方法修饰符.变量修饰符,其中每种修饰符又分为访问控制修饰符和非访问控制修饰符.访问控制存在的原因:a.让客户端程序员无法触及他们不应该触及的部分 ...

  4. 【目标检测】SSD:

    slides 讲得是相当清楚了: http://www.cs.unc.edu/~wliu/papers/ssd_eccv2016_slide.pdf 配合中文翻译来看: https://www.cnb ...

  5. LG4782 「模板」2-SAT问题 2-SAT

    问题描述 LG4782 题解 对于一个限制条件,建边如下: 如果\(x,-x\)在同一个强联通分量里,则不行,否则可以 构造方案:输出\(bel_i<bel_{i+n}\) \(\mathrm{ ...

  6. vector的基本操作

    vector怎么删除元素? #include<iostream> #include<vector> using namespace std; int main() { vect ...

  7. 前端Vue项目——首页/课程页面开发及Axios请求

    一.首页轮播图 1.elementUI走马灯 elementUI中 Carousel 走马灯,可以在有限空间内,循环播放同一类型的图片.文字等内容. 这里使用指示器样式,可以将指示器的显示位置设置在容 ...

  8. [LeetCode] 794. Valid Tic-Tac-Toe State 验证井字棋状态

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  9. 2.Python学习之路

    这里主要更新Python每个部分知识点的详细的目录...(知识点目录) Python基础 一.计算机基础 二.Python基础 三.函数 四.常用模块 五.模块和包 六.面向对象 Python进阶 七 ...

  10. 烦人的 Python 依赖

    pipreqs自动生成项目所需的组件目录 https://hub.docker.com/r/evanshawn/cloudreve/ https://www.cnblogs.com/baishucha ...