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. Django-rest-framework源码分析----权限

    添加权限 (1)API/utils文件夹下新建premission.py文件,代码如下: message是当没有权限时,提示的信息 # utils/permission.py class SVIPPr ...

  2. JAVA_SE基础——28.封装

    黑马程序员blog... 面向对象三大特征:1. 封装2. 继承3  多态. 今天我们先学习第一大特征,封装. 封装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 好处:     1. 将变 ...

  3. JAVA_SE基础——3.Java程序的开发流程

    上一篇,写的是JAVA的环境变量的配置,今天我抽空写篇Java程序的开发流程,下面的教程是我结合书本和毕向东老师的视频写下的心的~ 在没有真正写Java程序前,首先需要了解Java程序的开发过程. S ...

  4. Extensions in UWP Community Toolkit - Overview

    概述 UWP Community Toolkit  中有一个 Extensions 的集合,它们可以帮助开发者实现很多基础功能,省去自己造轮子的过程,本篇我们先来看一下 Extensions 的功能都 ...

  5. (第一章)对程序员来说CPU是什么

    这几天,看到一本书,<程序是怎么跑起来的>,觉得之前都没有完整的看完一本书,现在要从这本书开始,慢慢的培养自己写读书笔记的习惯,不能度过去就忘了. 学习是一个螺旋上升的过程,不要指望一下子 ...

  6. Python/零起点(一、数字及元组)

    Python/零起点(一.数字及元组) int整型 int()强行转换成整型数据类型 int整型是不可变,且是不可迭代的对象 一.整型数字用二进制位数表示案例: age=7 #设定一个数字赋值给age ...

  7. hive优化之——控制hive任务中的map数和reduce数

    一.    控制hive任务中的map数: 1.    通常情况下,作业会通过input的目录产生一个或者多个map任务.主要的决定因素有: input的文件总个数,input的文件大小,集群设置的文 ...

  8. [论文阅读] A Discriminative Feature Learning Approach for Deep Face Recognition (Center Loss)

    原文: A Discriminative Feature Learning Approach for Deep Face Recognition 用于人脸识别的center loss. 1)同时学习每 ...

  9. 浅谈linux静态库、动态库。

    动态库又叫动态共享文件(.so,Dynamic Shared Objects)和静态库(.a)都是将一些待重用的公共代码打包成一种特殊的重定位目标文件. 在使用时,连接器会将静态库中所有的代码,编译到 ...

  10. Python系列之 - 字符编码问题

    1.内存和硬盘都是用来存储的. CPU:速度快 硬盘:永久保存 2.文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就可以启动一个进程,是在内存中的,所以在编辑器编 ...