SpringBoot学习笔记:http接口请求
controller
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.domain.User;
@RestController
@RequestMapping("/get")
public class GetController {
Map result = new HashMap();
/**
* 测试GET请求1
* @return
*/
@RequestMapping(path="/test1", method=RequestMethod.GET)
public String test1() {
return "test1...";
}
/**
* 测试GET请求2(使用GetMapping简写)
* @return
*/
@GetMapping("/test2")
public String test2(){
return "test2...";
}
/**
* 测试GET请求参数传递及默认值
* @return
*/
@GetMapping("/test3")
public Object test3(@RequestParam(defaultValue="0",name="a") int a, int b){
result.clear();
result.put("a", a);
result.put("b", b);
return result;
}
/**
* 测试restful协议,从路径中获取字段
* @param cityId
* @param userId
* @return
*/
@GetMapping("/{city_id}/{user_id}")
public Object findUser(@PathVariable("city_id") String cityId,
@PathVariable("user_id") String userId) {
result.clear();
result.put("cityId", cityId);
result.put("userId", userId);
return result;
}
/**
* Bean对象传参
* 注意:1、要指定http请求头content-type为application/json
* 2、使用body传输数据
* @param user
* @return。
*/
@GetMapping("/saveuser")
public Object savaUser(@RequestBody User user){
result.clear();
result.put("user", user);
return result;
}
/**
* 获取http请求头部信息
* @param accessToken
* @param contentType
* @return
*/
@GetMapping("/headerinfo")
public Object headerinfo(@RequestHeader("access_token") String accessToken,
@RequestHeader("Content-Type") String contentType) {
result.clear();
result.put("access_token", accessToken);
result.put("content_type", contentType);
return result;
}
/**
* 获取request对象传递的参数
* @param request
* @return
*/
@GetMapping("/requestparams")
public Object requestparams(HttpServletRequest request){
result.clear();
result.put("param1", request.getParameter("param1"));
return result;
}
}
User实体类
package com.example.demo.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
@JsonProperty("name")
private String username;
@JsonIgnore
private String userid;
@JsonIgnore
private String password;
@JsonInclude(Include.NON_NULL)
private Integer age;
@JsonInclude(Include.NON_NULL)
@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss", locale="zh", timezone="GMT+8")
private Date createTime;
public User() {
super();
}郑州妇科医院哪家好 http://mobile.chfk120.com/
public User(String username, String userid, String password, Integer age, Date createTime) {
super();
this.username = username;
this.userid = userid;
this.password = password;
this.age = age;
this.createTime = createTime;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
SpringBoot学习笔记:http接口请求的更多相关文章
- SpringBoot学习笔记:Swagger实现文档管理
SpringBoot学习笔记:Swagger实现文档管理 Swagger Swagger是一个规范且完整的框架,用于生成.描述.调用和可视化RESTful风格的Web服务.Swagger的目标是对RE ...
- SpringBoot学习笔记:自定义拦截器
SpringBoot学习笔记:自定义拦截器 快速开始 拦截器类似于过滤器,但是拦截器提供更精细的的控制能力,它可以在一个请求过程中的两个节点进行拦截: 在请求发送到Controller之前 在响应发送 ...
- SpringBoot学习笔记
SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...
- SpringBoot学习笔记(3):静态资源处理
SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...
- SpringBoot学习笔记(2):引入Spring Security
SpringBoot学习笔记(2):用Spring Security来保护你的应用 快速开始 本指南将引导您完成使用受Spring Security保护的资源创建简单Web应用程序的过程. 参考资料: ...
- SpringBoot学习笔记(4):添加自定义的过滤器
SpringBoot:学习笔记(4)——添加自定义的过滤器 引入自定义过滤器 SpringBoot提供的前端控制器无法满足我们产品的需求时,我们需要添加自定义的过滤器. SpringBoot添加过滤器 ...
- SpringBoot学习笔记(10):使用MongoDB来访问数据
SpringBoot学习笔记(10):使用MongoDB来访问数据 快速开始 本指南将引导您完成使用Spring Data MongoDB构建应用程序的过程,该应用程序将数据存储在MongoDB(基于 ...
- SpringBoot学习笔记:动态数据源切换
SpringBoot学习笔记:动态数据源切换 数据源 Java的javax.sql.DataSource接口提供了一种处理数据库连接的标准方法.通常,DataSource使用URL和一些凭据来建立数据 ...
- springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源
本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...
- openresty 学习笔记二:获取请求数据
openresty 学习笔记二:获取请求数据 openresty 获取POST或者GET的请求参数.这个是要用openresty 做接口必须要做的事情.这里分几种类型:GET,POST(urlenco ...
随机推荐
- djang小项目过程中的小问题 01(django中的configrarion配置、django自带命名规范)
不知道为什么,明明照着做,但是我就会遇到一些错误问题,但是我觉着自己生下来就是解决问题的. ##1. 第一个是在django运行时,没有配置configration, 这个时候我添加一个django ...
- div层的滑入滑出实例
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat=&quo ...
- how to design AWS SQS?
遇到这么一题system design,怎么做? 几个月以前,有同事提出要用Webapi代替现有的WCF,当时我投的反对票.而且我给了很充分的理由,不仅仅是时间不足,人手不够,更重要的是这个变化太大, ...
- 【LG4437】[HNOI/AHOI2018]排列
[LG4437][HNOI/AHOI2018]排列 题面 洛谷 题解 题面里这个毒瘤的东西我们转化一下: 对于\(\forall k,j\),若\(p_k=a_{p_j}\),则\(k<j\). ...
- 8.10 NOIP模拟测试16 Blue+Weed+Drink
T1 Blue 贪心,每次跳得时候跳能跳到的最远的地方,跳过的就把他设为0,每次二分找到位置,一直跳就行,如果能跳到的位置就是当前位置或比当前位置还小(数组里现在呆着的这一块石头,二分得到的就是当前位 ...
- 7.29 NOIP模拟测试10 辣鸡(ljh)+模板(ac)+大佬(kat)
T1 辣鸡(ljh) 就是一道分类讨论的暴搜,外加一丢丢的减枝,然而我挂了,为啥呢,分类讨论变量名打错,大于小于号打反,能对才怪,写了sort为了调试就注释了,后来忘了解开,小减枝也没打.但是这道题做 ...
- 第04组 团队Git现场编程实战
组员职责分工 组员 分工 林涛(组长) 分配任务.整理数据.写博客 童圣滔 UI界面制作 林红莲 UI界面制作 潘雨佳 测评出福州最受欢迎的商圈 于瀚翔 测评出福州最受欢迎的商圈 覃鸿浩 测评出福州人 ...
- CodeChef MAXDTREE(DP套DP)
题意 链接:https://www.codechef.com/problems/MAXDTREE 给定一个 \(n\) 个节点的树,其中 \(1\) 为根节点,每个点有点权,我们定义" ...
- 分布式事物解决方案-TCC
分布式框架下,如何保证事物一致性一直是一个热门话题.当然事物一致性解决方案有很多种(请参考:分布式事物一致性设计思路),我们今天主要介绍TCC方案解决的思路.以下是参与设计讨论的一种解决思路,大家有问 ...
- docker-composer 简单教程
原文地址:https://blog.51cto.com/9291927/2310444 Docker快速入门——Docker-Compose 一.Docker-Compose简介 1.Docker-C ...