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时的处理的更多相关文章

  1. 8 — springboot中静态资源处理方式 - 前后端分离 这没屁用

    7中说了thymeleaf,哪还有一个目录是static 那么就来研究一下静态资源 静态资源,springboot底层是怎么去装配的,都在WebMvcAutoConfiguration有答案,去看一下 ...

  2. springboot中访问jsp文件方式

    首先,添加加载jsp文件的依赖包: <!--jsp依赖 对应springboot版本为2.1.4--><dependency> <groupId>org.apach ...

  3. Springboot中以配置类方式自定义Mybatis的配置规则(如开启驼峰映射等)

    什么是自定义Mybatis的配置规则? 答:即原来在mybatis配置文件中中我们配置到<settings>标签中的内容,如下第6-10行内容: 1 <?xml version=&q ...

  4. log4j 和 log4j2 在springboot中的性能对比

    文章链接: https://pengcheng.site/2019/11/17/log4j-he-log4j2-zai-springboot-zhong-de-xing-neng-dui-bi/ 前言 ...

  5. springboot成神之——mybatis在spring-boot中使用的几种方式

    本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...

  6. 在springboot中使用Mybatis Generator的两种方式

    介绍 Mybatis Generator(MBG)是Mybatis的一个代码生成工具.MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率.如果需要联合查询仍然需要手写sql. ...

  7. SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式

    在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...

  8. Springboot中IDE支持两种打包方式,即jar包和war包

    Springboot中IDE支持两种打包方式,即jar包和war包 打包之前修改pom.xml中的packaging节点,改为jar或者war    在项目的根目录执行maven 命令clean pa ...

  9. SpringBoot中的异常处理方式

    SpringBoot中有五种处理异常的方式: 一.自定义错误页面 SpringBoot默认的处理异常机制:SpringBoot默认的已经提供了一套处理异常的机制.一旦程序出现了异常SpringBoot ...

随机推荐

  1. Ubuntu命令行连接WPA/WPA2无线网线

    一,连接无加密无线网络zhang:sudo ip link set wlan0 up sudo iw dev wlan0 connect zhangsudo dhclient wlan0 二,连接WP ...

  2. HDFS文件读写操作(基础基础超基础)

    环境 OS: Ubuntu 16.04 64-Bit JDK: 1.7.0_80 64-Bit Hadoop: 2.6.5 原理 <权威指南>有两张图,下次po上来好好聊一下 实测 读操作 ...

  3. Mysql数据库mys和ora库的备份与恢复脚本

    !/bin/bash Time=$(date +%Y%md%H%M%S) Back_dir="$HOME/mysqlback/${Time}" function Detect_u_ ...

  4. 算法题丨Move Zeroes

    描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...

  5. Map集合、散列表、红黑树介绍

    前言 声明,本文用得是jdk1.8 前面已经讲了Collection的总览和剖析List集合: Collection总览 List集合就这么简单[源码剖析] 原本我是打算继续将Collection下的 ...

  6. Spring知识点回顾(08)spring aware

    Spring知识点回顾(08)spring aware BeanNameAware 获得容器中的bean名称 BeanFactoryAware 获得当前的bean factory Applicatio ...

  7. apigw鉴权分析(1-4)新浪微博开放平台 - 鉴权分析

    一.访问入口 http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E 微博开放接口的调用,如 ...

  8. Spring Security入门(3-4)Spring Security 异常处理、异常传递和异常获取

  9. spring9——AOP之AspectJ对AOP的实现

    从上述的实验中可以看出BeanNameAutoProxyCreator对于AOP的实现已经和完美了,但是还有两点不足之处: 1,对于切面的实现比较麻烦,既不同类型的通知切面要实现不同的接口,而且一个切 ...

  10. ZOJ-2913 Bus Pass---BFS进阶版

    题目链接: https://vjudge.net/problem/ZOJ-2913 题目大意: 问哪个区域到公交路线上所有区域的最大距离最小 思路: 这里要求出到底是哪个区域到某些指定区域的最大距离最 ...