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/ ...
随机推荐
- javascript中实现类似php 的var_dump
javascript语言中的调试功能少得可怜,如果涉及到第三方返回的对象数据更是使得开发程度加大.想到php中的var_dump,print_r简单好用,极大程序上方便了开发工作,在网上乱找一通,终于 ...
- HTML-loading动画1
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- tomcat7.0.55配置单向和双向HTTPS连接
HTTPS配置中分为单向连接和双向连接,单向连接只需要服务器安装证书,客户端不需要,双向连接需要服务器和客户端都安装证书 下面的配置都没有用CA签名来配置,都不能用于生产环境,实际配置中是需要CA的, ...
- facebook面试题【转】
1. 给两个类A和Bclass A {public void foo (A a) { ...}}class B extends A {public void foo (B b) { ...}}问这么写 ...
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- GRPC协议的相关原理
GRPC的Client与Server,均通过Netty Channel作为数据通信,序列化.反序列化则使用Protobuf,每个请求都将被封装成HTTP2的Stream,在整个生命周期中,客户端Cha ...
- HDU4372 Buildings
@(HDU)[Stirling數, 排列組合] Problem Description There are N buildings standing in a straight line in the ...
- Java使用logback记录日志时分级别保存文件
说明:一般情况下logback可以指定类使用什么样的级别显示输出日志,并且同一类可以指定不能级别,然后对应级别进行输出日志. 第一种配置: <?xml version="1.0&quo ...
- iOS Framework: Introducing MKNetworkKit (MKNetworkKit介绍,入门,翻译)
这片文章也有塞尔维亚-克罗地亚语(由Jovana Milutinovich翻译)和日语(由@noradaiko翻译) 如果有个一个网络库能够自动的为你处理cache该有多好啊. 如果有一个网络库能够在 ...
- c++ concurrency serial 1: introduction
platform: vs2012 Code#include <iostream> #include <thread> using namespace std; void Fun ...