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 ...
随机推荐
- thinkphp框架调用类不存在的方法
thinkphp框架调用类不存在的方法调用类不存在的方法,不会报错,但是也不会执行,这是根据tp框架里面的一个魔术方法,框架里面一共才十几个魔术方法
- ESP8266 wifi 模块配置,Wechat+APP控制实现
首先刷入安信可的AiCloud 2.0 SDK文件,AiCloud 2.0具体信息参见AiCloud 1.0 和AiCloud 2.0对比 APP见如下二维码下载. 1.安信可AiCloud 2.0 ...
- python入门:python包管理工具pip的安装
pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品. distribute是setuptools的取代(Setuptools包后期不再维护了),pip是e ...
- linux下的Shell编程(6)case和select
第一个,除了if语句之外,Shell Script中也有类似C语言中多分支结构的case语句,它的语法是: case var in pattern 1 ) - ;; pattern 2 ) - ;; ...
- springmvc4开发rest
Spring MVC 4 RESTFul Web Services CRUD Example+RestTemplate Created on: August 11, 2015 | Last upd ...
- spring-oauth-server实践:授权方式三:PASSWORD模式下 authorities:ROLE_{user.privillege}, ROLE_USER
一.数据库配置 1.oauth_client_details 2.user_ 3.user_privillege 二.password模式 授权过程 1.授权者granter和请求参数 Resourc ...
- Jetty入门(1-1)Jetty入门教程
一.Jetty是什么? 1.Jetty 是一个Java语言编写的,开源的Servlet容器和应用服务器. Jetty 极度轻量级.高便携性.功能强大.灵活和扩展性好,而且支持各种技术如SPDY.Web ...
- 实现GridControl行动态改变行字体和背景色
需求:开发时遇到一个问题, 需要根据GridControl行数据不同,实现不同的效果 在gridView的RowCellStyle的事件中实现,需要的效果 private void gridView1 ...
- NHibernate与IbatisNet的简单比较
NHibernate是当前最流行的Java O/R mapping框架Hibernate的移植版本,当前版本是1.0 rc-1.它出身于sf.net..IbatisNet是另外一种优秀的Java O/ ...
- vscode调试适配器已意外终止
出现这个错误了,找半天没找到办法.师兄支了一招: 把图中红圈部分删掉! 这是个旧的配置文件 ,你删掉它(反正一直报错误,也用不成了!).然后你调试一个文件,它会重新自动添加新的配置文件.