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/ ...
随机推荐
- Intellij IDEA创建项目
问题:无Java文件 解决方案: 打开file下面的project structure,在main目录下新建java文件,并将java文件设置为 Sources属性 问题:无resouces 解决方案 ...
- 虚拟机centos 同一个tomcat、不同端口访问不同的项目
在tomcat中复制webapps目录,并重命名:
- 前端笔记之Vue(七)Vue-router&axios&Vue插件&Mock.js&cookie|session&加密
一.Vue-router(路由) 1.1路由创建 官网:https://router.vuejs.org/zh/ 用 Vue.js + Vue Router 创建单页应用,是非常简单的.使用 Vue. ...
- 洛谷—— P1849 [USACO12MAR]拖拉机Tractor
https://www.luogu.org/problemnew/show/P1849 题目描述 After a long day of work, Farmer John completely fo ...
- Xamarin XAML语言教程对象元素的声明方式
Xamarin XAML语言教程对象元素的声明方式 XAML的对象元素的声明有两种形式,分别为包含属性的特性语法形式以及对象元素语法形式.在1.4小节中,我们看到了其中一种对XAML对象元素的声明方式 ...
- 着陆攻击LAND Attack
着陆攻击LAND Attack 着陆攻击LAND Attack也是一种拒绝服务攻击DOS.LAND是Local Area Network Denial的缩写,意思是局域网拒绝服务攻击,翻译为着陆攻 ...
- [Bzoj4942][Noi2017]整数(线段树)
4942: [Noi2017]整数 Time Limit: 50 Sec Memory Limit: 512 MBSubmit: 363 Solved: 237[Submit][Status][D ...
- Mybatis Generator插件和PageHelper使用
最近,开始接触web项目开发,项目使用springboot和mybatis,以前一直以为开发过程中实体类,mybatis的xml文件都需要自己手动的去创建. 同事推荐说Mybatis Generato ...
- iOS --SKView类
SKView类 继承自 UIView:UIResponder:NSObject 符合 NSCoding(UIView)UIAppearance(UIView)UIAppearanceContainer ...
- [c++菜鸟]《Accelerate C++》习题解答
第0章 0-0 编译并运行Hello, world! 程序. #include <iostream> using namespace std; int main() { cout < ...