一,无参接口:

    //无参接口
@RequestMapping("/appSecret")
public String secret() {
return "EK125EKLNGKNELKGKGNKLEGNK87";
}

  访问接口

  

二,带参接口:

 @RequestMapping("/serviceTime")
public String time(@RequestParam(value = "local", required = true) String local) {
System.out.println("local:"+local);
return "2018-8-8 18:36:00";
}

  访问接口

  

  

三,多参接口

//多参接口,表单
@RequestMapping("/register")
public Account register(String username, String password) {
Account user = new Account();
user.setUsername(username);
user.setPassword(password);
return user;
}

  访问接口

  

四,json实例对象

//json实体对象
@RequestMapping(value = "/addAccount", method = RequestMethod.POST)
public Account addAccount(@RequestBody Account account) {
System.out.print(account.getUsername());
return account;
}

  访问接口:

  

五,路径参数:

//路径参数
@RequestMapping(value="/searchAccountById/{id}",method = RequestMethod.POST)
public String searchAccountById(@PathVariable("id") int id) {
return "{id:"+id+"}";
}
@RequestMapping(value="/formatDate/{year}-{month}-{day}",method = RequestMethod.POST)
public String formatDate(@PathVariable("year") int year, @PathVariable("month") int month, @PathVariable("day") int day) {
return year + "年" + month + "月" + day + "日";
}

  访问接口

  

  

Controller代码:

package com.example.demo.controllers;

import com.example.demo.domain.Account;
import org.springframework.web.bind.annotation.*; /**
* Created by zhang_guang_yang on 2018/11/18.
*/
@RestController
public class UserBusinessController { //无参接口
@RequestMapping("/appSecret")
public String secret() {
return "EK125EKLNGKNELKGKGNKLEGNK87";
} //带参接口
@RequestMapping("/serviceTime")
public String time(@RequestParam(value = "local", required = true) String local) {
System.out.println("local:"+local);
return "2018-8-8 18:36:00";
} //多参接口,表单
@RequestMapping("/register")
public Account register(String username, String password) {
Account user = new Account();
user.setUsername(username);
user.setPassword(password);
return user;
} //json实体对象
@RequestMapping(value = "/addAccount", method = RequestMethod.POST)
public Account addAccount(@RequestBody Account account) {
System.out.print(account.getUsername());
return account;
} //路径参数
@RequestMapping(value="/searchAccountById/{id}",method = RequestMethod.POST)
public String searchAccountById(@PathVariable("id") int id) {
return "{id:"+id+"}";
}
@RequestMapping(value="/formatDate/{year}-{month}-{day}",method = RequestMethod.POST)
public String formatDate(@PathVariable("year") int year, @PathVariable("month") int month, @PathVariable("day") int day) {
return year + "年" + month + "月" + day + "日";
} }

补充: Map类型多参数转换,POST,GET接口的实现:

package com.ams.accountmanagementsystem.controllers;

import com.ams.accountmanagementsystem.models.TestUser;
import org.springframework.web.bind.annotation.*; import java.util.Map; @RestController
public class ApiTest { // 不带参数
@RequestMapping("/test_no_param")
public String testNoParam() {
return "success";
} // 带有一个参数
@RequestMapping("/test_param")
public String testParam(@RequestParam String param) {
return "success, the param is: " + param;
} // 带多个参数
@RequestMapping("/test_multiple_param")
public String testMultipleParam(@RequestParam String name, @RequestParam String password) {
return "success, the name is: " + name + " password is: " + password;
} // map接受参数
@RequestMapping("/test_param_map")
public String testParamMap(@RequestParam Map map) {
return "success, map: " + map;
} // path参数
@RequestMapping("/test/{path}")
public String testPath(@PathVariable String path) {
return "success, path: " + path;
} // post带一个参数
@PostMapping("/test_post_param")
public String postParam(@RequestBody String time) {
return "success, time: " + time;
} // post带Map参数
@PostMapping("/test_post_map_param")
public String postMapParam(@RequestBody Map map) {
return "success, map: " + map;
} // post转实体对象
@PostMapping("/test_post_entity")
public String postEntity(@RequestBody TestUser user) {
return "success, entity " + user;
} // POST GET
@RequestMapping(value = "/test_get", method = RequestMethod.GET)
public String testGetMethod() {
return "get method";
} @RequestMapping(value = "/test_post", method = RequestMethod.POST)
public String testPostMethod() {
return "post method";
} @GetMapping("/test_get_method")
public String testGet() {
return "get method";
} @PostMapping("/test_post_method")
public String testPost() {
return "post method";
}
}

SpringBoot-(3)-RestController接口参数的更多相关文章

  1. 【快学springboot】4.接口参数校验

    前言 在开发接口的时候,参数校验是必不可少的.参数的类型,长度等规则,在开发初期都应该由产品经理或者技术负责人等来约定.如果不对入参做校验,很有可能会因为一些不合法的参数而导致系统出现异常. 上一篇文 ...

  2. SpringBoot实现通用的接口参数校验

    本文介绍基于Spring Boot和JDK8编写一个AOP,结合自定义注解实现通用的接口参数校验. 缘由 目前参数校验常用的方法是在实体类上添加注解,但对于不同的方法,所应用的校验规则也是不一样的,例 ...

  3. java springboot调用第三方接口 借助hutoool工具类 爬坑

    楼主是个后端小白一枚,之前没接触过后端,只学了java基本语法,还是在学校老师教的,学的很浅,什么ssh.ssm框架都没有学,最近在自学spring boot,看书学也看不是很懂,就在b站上看教学视频 ...

  4. 【快学springboot】2.Restful简介,SpringBoot构建Restful接口

    Restful简介 Restful一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现 ...

  5. SpringBoot写后端接口,看这一篇就够了!

    摘要:本文演示如何构建起一个优秀的后端接口体系,体系构建好了自然就有了规范,同时再构建新的后端接口也会十分轻松. 一个后端接口大致分为四个部分组成:接口地址(url).接口请求方式(get.post等 ...

  6. SpringBoot 如何生成接口文档,老鸟们都这么玩的!

    大家好,我是飘渺. SpringBoot老鸟系列的文章已经写了两篇,每篇的阅读反响都还不错,果然大家还是对SpringBoot比较感兴趣.那今天我们就带来老鸟系列的第三篇:集成Swagger接口文档以 ...

  7. SpringBoot下支付宝接口的使用

    SpringBoot下支付宝接口的使用 前期准备: 参考之前写过的 支付宝接口引入servlet版本 Jar包引入: <!-- 支付宝 --> <dependency> < ...

  8. springboot返回统一接口与统一异常处理

    springboot返回统一接口与统一异常处理 编写人员:yls 编写时间:2019-9-19 0001-springboot返回统一接口与统一异常处理 简介 创建统一的返回格式 Result 封装统 ...

  9. Spring Boot 之:接口参数校验

    Spring Boot 之:接口参数校验,学习资料 网址 SpringBoot(八) JSR-303 数据验证(写的比较好) https://qq343509740.gitee.io/2018/07/ ...

随机推荐

  1. TextReader 和StreamReader

    TextReader 和StreamReader 目录: 为什么要介绍 TextReader? TextReader的常用属性和方法 TextReader 示例 从StreamReader想到多态 简 ...

  2. 在Eclipse中打开Hadoop工程

    1. 安装虚拟机,我用的是VMware Workstation 12 Player 2. 在VM中安装Ubuntu,我用的镜像文件是ubuntu-15.10-desktop-amd64.iso 3. ...

  3. Java 实现随机验证码

    许多系统的注册.登录或者发布信息模块都添加的随机码功能,就是为了避免自动注册程序或者自动发布程序的使用. 验证码实际上就是随机选择一些字符以图片的形式展现在页面上,如果进行提交操作的同时需要将图片上的 ...

  4. OS | monolithic kernel & microkernel

    A monolithic kernel is a kernel where all services (file system, VFS, device drivers, etc) as well a ...

  5. Java 对象的序列化、反序列化

    对象的序列化(Serialize):将内存中的Java对象转换为与平台无关的二进制流(字节序列),然后存储在磁盘文件中,或通过网络传输给另一个网络节点. 对象的反序列化(Deserialize):获取 ...

  6. Java---详解方法传值问题

    过程解析: 1.首先执行int[] arr={3,5,6,1,7,9,0},遇到数组先执行等式右边的,{3,5,6,1,7,9,0}会在堆内存中开辟一块空间,分成7小块,下标分别从0~6,先进行系统初 ...

  7. this.class.getClassLoader().getResourceAsStream

    this.getClass().getClassLoader().getResource("template");    首先,调用对象的getClass()方法是获得对象当前的类 ...

  8. sqlserverinternals.com

    http://sqlblog.com/blogs/kalen_delaney/default.aspx https://sqlserverinternals.com/

  9. 【bootstrap】bootstrap中的tooltip的使用

    先看看效果图:[当光标放在下面这个时间搜索框上时,显示一段文字:搜索时间段中的流水信息] 这样的效果,怎么实现呢? 很简单 1.引入jQuery.js和bootstrap的js和css 2.给想要有t ...

  10. Android -- 开机启动无界面后台程序

    简单的说,这是一个没有界面的后台运行的应用程序 而且,还有一功能:开机自启动,启动运行一个服务 程序结构非常简单,两个类,一个是service的扩展类,一个是BroadcastReceiver扩展类 ...