SpringBoot-(3)-RestController接口参数
一,无参接口:
//无参接口
@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接口参数的更多相关文章
- 【快学springboot】4.接口参数校验
前言 在开发接口的时候,参数校验是必不可少的.参数的类型,长度等规则,在开发初期都应该由产品经理或者技术负责人等来约定.如果不对入参做校验,很有可能会因为一些不合法的参数而导致系统出现异常. 上一篇文 ...
- SpringBoot实现通用的接口参数校验
本文介绍基于Spring Boot和JDK8编写一个AOP,结合自定义注解实现通用的接口参数校验. 缘由 目前参数校验常用的方法是在实体类上添加注解,但对于不同的方法,所应用的校验规则也是不一样的,例 ...
- java springboot调用第三方接口 借助hutoool工具类 爬坑
楼主是个后端小白一枚,之前没接触过后端,只学了java基本语法,还是在学校老师教的,学的很浅,什么ssh.ssm框架都没有学,最近在自学spring boot,看书学也看不是很懂,就在b站上看教学视频 ...
- 【快学springboot】2.Restful简介,SpringBoot构建Restful接口
Restful简介 Restful一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现 ...
- SpringBoot写后端接口,看这一篇就够了!
摘要:本文演示如何构建起一个优秀的后端接口体系,体系构建好了自然就有了规范,同时再构建新的后端接口也会十分轻松. 一个后端接口大致分为四个部分组成:接口地址(url).接口请求方式(get.post等 ...
- SpringBoot 如何生成接口文档,老鸟们都这么玩的!
大家好,我是飘渺. SpringBoot老鸟系列的文章已经写了两篇,每篇的阅读反响都还不错,果然大家还是对SpringBoot比较感兴趣.那今天我们就带来老鸟系列的第三篇:集成Swagger接口文档以 ...
- SpringBoot下支付宝接口的使用
SpringBoot下支付宝接口的使用 前期准备: 参考之前写过的 支付宝接口引入servlet版本 Jar包引入: <!-- 支付宝 --> <dependency> < ...
- springboot返回统一接口与统一异常处理
springboot返回统一接口与统一异常处理 编写人员:yls 编写时间:2019-9-19 0001-springboot返回统一接口与统一异常处理 简介 创建统一的返回格式 Result 封装统 ...
- Spring Boot 之:接口参数校验
Spring Boot 之:接口参数校验,学习资料 网址 SpringBoot(八) JSR-303 数据验证(写的比较好) https://qq343509740.gitee.io/2018/07/ ...
随机推荐
- LeetCode OJ——Subsets
http://oj.leetcode.com/problems/subsets/ 计算一个集合的子集,使用vector<vector<int> >,使用了进制的思想. #inc ...
- AC日记——[网络流24题]骑士共存 cogs 746
746. [网络流24题] 骑士共存 ★★☆ 输入文件:knight.in 输出文件:knight.out 简单对比时间限制:1 s 内存限制:128 MB 骑士共存问题 «问题描述: ...
- AC日记——最大子树和 洛谷 P1122
题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...
- lodash常用
1) Loop for N times // 1. Basic for loop. for(var i = 0; i < 5; i++) { // .... } // 2. Using Arra ...
- Android Base64转图片
最近做一个项目里面有关于图片展示的需求,但是任性的后台跟我说没有图片服务器,只能给我base64让我自己转成图片,好吧,我忍,转就转吧.. 首先第一步咱还是谦虚点上百度查查别人咋转的,结果似乎各位码友 ...
- Android -- AsyncTask 使用和缺陷
一.AsyncTask的基本用法 由于AsyncTask是一个抽象类,所以如果我们想使用它,就必须要创建一个子类去继承它.在继承时我们可以为AsyncTask类指定三个泛型参数,这三个参数的用途如下: ...
- 线性回归,logistic回归分类
学习过程 下面是一个典型的机器学习的过程,首先给出一个输入数据,我们的算法会通过一系列的过程得到一个估计的函数,这个函数有能力对没有见过的新数据给出一个新的估计,也被称为构建一个模型.就如同上面的线性 ...
- 首先使用flex制作table
HTML(JavaScript) <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- 文本聚类——Kmeans
上两篇文章分别用朴素贝叶斯算法和KNN算法对newgroup文本进行了分类測试.本文使用Kmeans算法对文本进行聚类. 1.文本预处理 文本预处理在前面两本文章中已经介绍,此处(略). 2.文本向量 ...
- SpringMVC:JSON
@ResponseBody params="json":访问我这个方法的时候一定要有参数名为json 返回值Userjackson-all-1.9.0.jar @RequestMa ...