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页面,这 ...
随机推荐
- 转:UINavigationBar返回上一级出现nested pop animation can result in corrupted navigation bar
[self.navigationController popViewControllerAnimated:NO]; 出现上面的错误是因为pop的时候要确保先让本页面加载完成,即如果在viewDidLo ...
- Git之简介及安装
简介 Git是一个分布式版本控制系统,GitHub相当于一个远程仓库,注册账号可免费获得Git远程仓库. GitHub使用参考:https://guides.github.com/activities ...
- SVN搭建和使用
原文出处: http://www.cnblogs.com/tugenhua0707/p/3969558.html SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不 ...
- [转载]ECMA-262 6th Edition / Draft August 24, 2014 Draft ECMAScript Language Specification
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-23.4 Draft Report Errors and Issues at: htt ...
- 【AtCoder Grand Contest 012C】Tautonym Puzzle [构造]
Tautonym Puzzle Time Limit: 50 Sec Memory Limit: 256 MB Description 定义一个序列贡献为1,当且仅当这个序列 由两个相同的串拼接而成 ...
- 上海支付宝终面后等了两周,没能收到offer却来了杭州淘宝的电话面试
上上周一(14/12/22)上海支付宝hr终面 http://www.cnblogs.com/zhanghaoh/p/4178386.html 苦苦等了两周,没能如愿收到offer,却在今天等来了 杭 ...
- Solr管理索引库——(十三)
a) 维护索引 1. 添加/更新文档 添加或更新单个文档
- 脚本病毒分析扫描专题1-VBA代码阅读扫盲、宏病毒分析
1.Office Macor MS office宏的编程语言是Visual Basic For Applications(VBA). 微软在1994年发行的Excel5.0版本中,即具备了VBA的宏功 ...
- XXX变种-防火墙放行自身
1.利用防火墙命令放行自身手法 netsh firewall add allowedprogram "C:\Users\USER\AppData\Local\Temp\Discord Can ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...