一,无参接口:

    //无参接口
@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";
}
}

最新文章

  1. python3网络爬虫笔记
  2. tp框架简易导出数据库
  3. UML3
  4. 记录linux系统下所有用户的操作信息
  5. StringUtil
  6. 深入GetMessage和PeekMessage
  7. DevExpress之ASPxGridView笔记(1)
  8. 传入sql数组字符串,输出table
  9. easyUI之tree
  10. Mongodb 之insert瞬时完成,测试数据---飞天博客
  11. 201521123099 《Java程序设计》 第10周学习总结
  12. The Moving Points HDU - 4717
  13. [Luogu2991][USACO10OPEN]水滑梯Water Slides
  14. Pythoy 数据类型序列化——json&pickle 模块
  15. Numbers
  16. jQuery的收尾
  17. POJ 3415 Common Substrings 【长度不小于 K 的公共子串的个数】
  18. go语言之进阶篇接口的继承
  19. 在“开始”菜单中的“运行”一栏输入特定命令打开windows程序
  20. Oracle EBS AR 更新客户配置文件

热门文章

  1. 本地测试IIS,Post调用接口
  2. jquery鼠标点击窗口或浮动层以外关闭层【阻止冒泡事件】
  3. luogu P1879 [USACO06NOV]玉米田Corn Fields
  4. TF-IDF学习笔记
  5. .net 哈希
  6. <LeetCode OJ> 204. Count Primes
  7. Android--------------几个ADB经常使用命令
  8. -webkit-transform:translate3d(0,0,0)触发GPU加速,让网页动画更流畅
  9. STP 根桥、根port、指定port是怎样选举的
  10. 基于ACCESS和ASP的SQL多个表查询与计算统计代码(一)