美文网首页
几分钟写一个接口

几分钟写一个接口

作者: iceIC | 来源:发表于2018-01-24 11:01 被阅读24次

请先戳此处按照教程创建一个项目 [如果你只是想简单的入门,只需浏览完 初始化SpringBoot 篇幅即可]

简单Demo

@Controller
@EnableAutoConfiguration
class Test {
    @RequestMapping("/")
    @ResponseBody
    fun home(request: HttpServletRequest): String {
        return "hello world!"
    }
}

fun main(args: Array<String>) {
    SpringApplication.run(Test::class.java, *args)
}

http://localhost:8080


POST请求

@RequestMapping(value = "/",method = arrayOf(RequestMethod.POST))
@ResponseBody
fun home(request: HttpServletRequest): String {
    return "hello world!"
}

RestFul

@RequestMapping(value = "/{name}")
@ResponseBody
fun home(@PathVariable name: String, request: HttpServletRequest): String {
    return "hello world ! $name"
}

http://localhost:8080/ice


请求参数

GET
@RequestMapping("/")
@ResponseBody
fun home(@RequestParam(defaultValue = "ice") name: String, request: HttpServletRequest): String {
    return "hello world ! $name"
}

http://localhost:8080/?name=IC

POST
@RequestMapping("/",method = arrayOf(RequestMethod.POST))
@ResponseBody
fun home(@RequestParam(defaultValue = "ice") name: String, request: HttpServletRequest): String {
    return "hello world ! $name"
}
image.png

传递JSON串

@RequestMapping("/", method = arrayOf(RequestMethod.POST))
@ResponseBody
fun home(@RequestBody loginBean: LoginBean, request: HttpServletRequest): String {
    return "$loginBean"
}
data class LoginBean(var account: String, var pwd: String){
    constructor() : this("ice","IC")
}
image.png

以上就是基本的写法了,如果有什么额外的需求,欢迎留言。


相关文章

  • 几分钟写一个接口

    请先戳此处按照教程创建一个项目 [如果你只是想简单的入门,只需浏览完 初始化SpringBoot 篇幅即可] 简单...

  • 栈的实现(基于链表)

    先定义一个接口 写一个栈的类,继承Stack接口

  • ItemTouchHelper.callback用法

    写一个接口 适配器实现接口 给接口定义的形参赋值写一个类继承ItemTouchHelper 设置上下左右移动替换删...

  • 规范使用pymysql

    前几天写的一个flask接口部署到服务器上,前几分钟请求一直没问题,但是每次到了30分钟之后就各种报错,后来找了半...

  • java中的动态代理Proxy

    创建一个 UserService 接口 写一个 UserService 接口的具体实现类 UserServiceI...

  • python中用flask写数据接口

    1、flask 写接口 第一次写数据接口,采用 flask 框架,还挺简单。只需几行代码就能完成一个简单的接口,例...

  • java中如何写一个接口并实现

    java中如何写一个接口并实现 接口的定义方式如下: [可见度] interface接口名称 [extends其他...

  • Laravel 统一API设计

    最近要写一套系统接口,发现公司里写Java的同事写的接口提供的都是统一个请求地址,因为以前没有写过接口,感觉挺有意...

  • Go实战项目【五】登录注册接口开发和JWT

    用户模型已经构建完了,可以愉快的写接口开发了,来编写第一个接口,登录/注册接口 首先定义接口routers/rou...

  • Postman 接口测试(测试用例)

    在 Postman 里面写接口测试用例其实就是写测试脚本。下面我们来学习如何写一个简单的接口测试用例。 1、获取 ...

网友评论

      本文标题:几分钟写一个接口

      本文链接:https://www.haomeiwen.com/subject/lxiyaxtx.html