spring boot get和post请求,以及requestbody为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);
- }
- }
spring boot get和post请求,以及requestbody为json串时候的处理的更多相关文章
- Spring Boot中扩展XML请求和响应的支持
在Spring Boot中,我们大多时候都只提到和用到了针对HTML和JSON格式的请求与响应处理.那么对于XML格式的请求要如何快速的在Controller中包装成对象,以及如何以XML的格式返回一 ...
- Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理
Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理 本文链接:https://blog.csdn.net/puhaiyang/article/details/78146620 ...
- spring boot aop打印http请求回复日志包含请求体
一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- spring boot中 使用http请求
因为项目需求,需要两个系统之间进行通信,经过一番调研,决定使用http请求. 服务端没有什么好说的,本来就是使用web 页面进行访问的,所以spring boot启动后,controller层的接口就 ...
- Spring Boot + Vue 跨域请求问题
使用Spring Boot + Vue 做前后端分离项目搭建,实现登录时,出现跨域请求 Access to XMLHttpRequest at 'http://localhost/open/login ...
- 【轮询】【ajax】【js】【spring boot】ajax超时请求:前端轮询处理超时请求解决方案 + spring boot服务设置接口超时时间的设置
场景描述: ajax设置timeout在本机测试有效,但是在生产环境等外网环境无效的问题 1.ajax的timeout属性设置 前端请求超时事件[网络连接不稳定时候,就无效了] var data = ...
- spring boot @Schedule 如何定时请求任务
spring boot 定时任务,添加两个注解即可: 1,启动类添加:@EnableScheduling 2,方法上添加注解:@Scheduled(cron = "0/5 * * * * ? ...
- 【Spring学习笔记-MVC-6】SpringMVC 之@RequestBody 接收Json数组对象
作者:ssslinppp 1. 摘要 程序流程: 前台使用ajax技术,传递json字符串到后台: 后台使用Spring MVC注解@RequestBody 接受前台传递的json字符串, ...
- Spring Boot教程(二十五)返回JSON格式
在上述例子中,通过@ControllerAdvice统一定义不同Exception映射到不同错误处理页面.而当我们要实现RESTful API时,返回的错误是JSON格式的数据,而不是HTML页面,这 ...
随机推荐
- linux diff 命令
diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...
- Codeforces 877 D. Olya and Energy Drinks
http://codeforces.com/contest/877/problem/D D. Olya and Energy Drinks time limit per test 2 second ...
- bzoj 3309 反演
$n=p_1^{a_1}p_2^{a_2}…p_k^{a_k},p_i$为素数,定义$f(n)=max(a_1,a_2…,a_k)$. 给定a,b<=1e7求$\sum\limits_{i=1} ...
- 八、Kafka总结
一 Kafka概述 1.1 Kafka是什么 在流式计算中,Kafka一般用来缓存数据,Storm通过消费Kafka的数据进行计算. 1)Apache Kafka是一个开源消息系统,由Scala写成. ...
- Grafana关键词
The open platform for beautiful analytics and monitoring. data source.panels.apps.dashboards. Organi ...
- Python入门系列教程(二)字符串
字符串 1.字符串输出 name = 'xiaoming' print("姓名:%s"%name) 2.字符串输入 userName = raw_input('请输入用户名:') ...
- golang sql.DB
数据库 sql.DB连接池需知: sql.DB内置连接池,连接不足时会自动创建新连接,新创建的连接使用sql.Open()时传入的dsn来构造. sql.DBClose时只会关闭连接池中的连接,未归还 ...
- es6解构、中括号前加分号
在写项目的时候,为了方便使用了下对象的解构,无奈又遇到一坑. 为什么会不能解构呢?因为这里的{}会导致歧义,因为 JavaScript 引擎会将{xxxxx}理解成一个代码块,从而发生语法错误.只有不 ...
- IE的双边距Bug以及解决办法
display:inline和display:block区别 一.什么是双边距Bug? 先来看图: 我们要让绿色盒模型在蓝色盒模型之内向左浮动,并且距蓝色盒模型左侧100像素.这个例子很常见,比如在网 ...
- java中并发Queue种类与各自API特点以及使用场景!
一 先说下队列 队列是一种数据结构.它有两个基本操作:在队列尾部加入一个元素,和从队列头部移除一个元素(注意不要弄混队列的头部和尾部) 就是说,队列以一种先进先出的方式管理数据,如果你试图向一个 已经 ...