SpringBoot 中 get/post 请求处理方式,以及requestboy为Json时的处理
GET、POST方式提时, 根据request header Content-Type的值来判断:
- application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
- multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
- 其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);
package com.example.controller; 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.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.bean.RequestLoginBean;
import com.example.response.BaseResponse;
import com.google.gson.Gson; @RestController
@RequestMapping(value = "/index")
public class Login { /**
* index home
*
* @return
*/
@RequestMapping(value = "/home")
public String home() {
return "index home";
} /**
* 得到1个参数
*
* @param name
* 用户名
* @return 返回结果
*/
@GetMapping(value = "/{name}")
public String index(@PathVariable String name) {
return "oh you are " + name + "<br> nice to meet you";// \n不起作用了,那就直接用html中的标签吧
} /**
* 简单post请求
*
* @param name
* @param pwd
* @return
*/
@RequestMapping(value = "/testpost", method = RequestMethod.POST)
public String testpost() {
System.out.println("hello test post");
return "ok";
} /**
* 同时得到两个参数
*
* @param name
* 用户名
* @param pwd
* 密码
* @return 返回结果
*/
@GetMapping(value = "/login/{name}&{pwd}")
public String login(@PathVariable String name, @PathVariable String pwd) {
if (name.equals("admin") && pwd.equals("admin")) {
return "hello welcome admin";
} else {
return "oh sorry user name or password is wrong";
}
} /**
* 通过get请求去登陆
*
* @param name
* @param pwd
* @return
*/
@RequestMapping(value = "/loginbyget", method = RequestMethod.GET)
public String loginByGet(@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "pwd", required = true) String pwd) {
return login4Return(name, pwd);
} /**
* 通过post请求去登陆
*
* @param name
* @param pwd
* @return
*/
@RequestMapping(value = "/loginbypost", method = RequestMethod.POST)
public String loginByPost(@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "pwd", required = true) String pwd) {
System.out.println("hello post");
return login4Return(name, pwd);
} /**
* 参数为一个bean对象.spring会自动为我们关联映射
* @param loginBean
* @return
*/
@RequestMapping(value = "/loginbypost1", method = { RequestMethod.POST, RequestMethod.GET })
public String loginByPost1(RequestLoginBean loginBean) {
if (null != loginBean) {
return login4Return(loginBean.getName(), loginBean.getPwd());
} else {
return "error";
}
} /**
* 请求内容是一个json串,spring会自动把他和我们的参数bean对应起来,不过要加@RequestBody注解
*
* @param name
* @param pwd
* @return
*/
@RequestMapping(value = "/loginbypost2", method = { RequestMethod.POST, RequestMethod.GET })
public String loginByPost2(@RequestBody RequestLoginBean loginBean) {
if (null != loginBean) {
return login4Return(loginBean.getName(), loginBean.getPwd());
} else {
return "error";
}
} /**
* 对登录做出响应处理的方法
*
* @param name
* 用户名
* @param pwd
* 密码
* @return 返回处理结果
*/
private String login4Return(String name, String pwd) {
String result;
BaseResponse response = new BaseResponse();
if (name.equals("admin") && pwd.equals("admin")) {
result = "hello welcome admin";
response.setState(true);
} else {
result = "oh sorry user name or password is wrong";
response.setState(false);
}
System.out.println("收到请求,请求结果:" + result);
return new Gson().toJson(response);
}
}
SpringBoot 中 get/post 请求处理方式,以及requestboy为Json时的处理的更多相关文章
- 8 — springboot中静态资源处理方式 - 前后端分离 这没屁用
7中说了thymeleaf,哪还有一个目录是static 那么就来研究一下静态资源 静态资源,springboot底层是怎么去装配的,都在WebMvcAutoConfiguration有答案,去看一下 ...
- springboot中访问jsp文件方式
首先,添加加载jsp文件的依赖包: <!--jsp依赖 对应springboot版本为2.1.4--><dependency> <groupId>org.apach ...
- Springboot中以配置类方式自定义Mybatis的配置规则(如开启驼峰映射等)
什么是自定义Mybatis的配置规则? 答:即原来在mybatis配置文件中中我们配置到<settings>标签中的内容,如下第6-10行内容: 1 <?xml version=&q ...
- log4j 和 log4j2 在springboot中的性能对比
文章链接: https://pengcheng.site/2019/11/17/log4j-he-log4j2-zai-springboot-zhong-de-xing-neng-dui-bi/ 前言 ...
- springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...
- 在springboot中使用Mybatis Generator的两种方式
介绍 Mybatis Generator(MBG)是Mybatis的一个代码生成工具.MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率.如果需要联合查询仍然需要手写sql. ...
- SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式
在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...
- Springboot中IDE支持两种打包方式,即jar包和war包
Springboot中IDE支持两种打包方式,即jar包和war包 打包之前修改pom.xml中的packaging节点,改为jar或者war 在项目的根目录执行maven 命令clean pa ...
- SpringBoot中的异常处理方式
SpringBoot中有五种处理异常的方式: 一.自定义错误页面 SpringBoot默认的处理异常机制:SpringBoot默认的已经提供了一套处理异常的机制.一旦程序出现了异常SpringBoot ...
随机推荐
- css3兼容IE8的方案 各个ie的hack
虽然现在很多项目已经对低版本IE不要求了,但是还有部分公司对IE8还是很执着的,咱作为屌丝前端程序员不能和老板说前端潮流,不能说趋势,只能动脑子了,下面就分享一些css3兼容ie8的方案思路.主要是实 ...
- java 数组排序方法整理,简单易懂,
1.快速排序:首先是最简单的Array.sort,直接进行排序: public static void main(String[] args) { int[] arr = {4,3,5,1,7,9,3 ...
- 高级OOP特性(6)
PHP不支持的高级OPP特性 PHP不支持通过函数重载实现多态 PHP不支持多重继承 PHP不支持根据所修改数据类型为操作符赋予新的含义 对象克隆 克隆实例 在对象前面添加clone关键字来克隆对象, ...
- Django中自定义过滤器的使用
我在这里做的是: 从数据库查出id递增的一些信息,展示在前台. 编写一个过滤器判断查出数据的id是偶数的返回True 奇数返回False 1 创建项目,创建应用,注册应用,配置settings.py文 ...
- oracle中求1到100之间的质数和
declare i number:=1; j number:=0; sum1 number:=0;begin while(i<100) loop i:=i+1; j:=2; while(mod( ...
- django Form组件 上传文件
上传文件 注意:FORM表单提交文件要有一个参数enctype="multipart/form-data" 普通上传: urls: url(r'^f1/',views.f1), u ...
- [论文阅读]VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION(VGGNet)
VGGNet由牛津大学的视觉几何组(Visual Geometry Group)提出,是ILSVRC-2014中定位任务第一名和分类任务第二名.本文的主要贡献点就是使用小的卷积核(3x3)来增加网络的 ...
- requests-文件上传
import requests files = {'file':open('D://tomas.jpg','rb')}#设定一个files,打开文件对象 response = requests.pos ...
- Spring源码分析:Spring IOC容器初始化
概述: Spring 对于Java 开发来说,以及算得上非常基础并且核心的框架了,在有一定开发经验后,阅读源码能更好的提高我们的编码能力并且让我们对其更加理解.俗话说知己知彼,百战不殆.当你对Spri ...
- TNS-12560: Message 12560 not found; No message file for product=network, facility=TNS报错
[oracle@localhost bin]$ ./lsnrctl startLSNRCTL for Linux: Version 12.2.0.1.0 - Production on 17-APR- ...