几分钟写一个接口

请先戳此处按照教程创建一个项目 [如果你只是想简单的入门,只需浏览完 初始化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

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


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • 巨啰嗦的框架:Paste + PasteDeploy + Routes + WebOb。后来OpenStack社区...
    Programmer客栈阅读 700评论 0 0
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,287评论 25 708
  • 我像个正人君子一样的开始叙述,在那一年的冬天 爱上一个如天蓝般的女孩。 章一:开学季 泪别青春最苦最美好的岁月。 ...
    Penn先生阅读 454评论 2 4
  • 最简单教程更新时间 : 2016.05.24 以下均为终端命令 1. 替换源 2. 更新 gem (输入密码) 3...
    Ayler_Lee阅读 318评论 0 0