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页面,这 ...
随机推荐
- 转:EasyJSWebView
EasyJSWebView 是类似 Android javascriptInterface 的 uiwebview js 调用原生代码框架 示例代码: 先建一个MyJSInterface接口 @in ...
- python---堡垒机开发
一:堡垒机需求分析 注意: 虽然我们在中间使用防火墙服务器对流量进行拦截和转发也可以起到过滤作用,但是我们无法去获取到完整,正确的操作记录.因为无论是客户端还是服务器端(管理员可能会去修改记录,而且可 ...
- iview组件 eslint校验出错 Parsing error: x-invalid-end-tag
如下: 解决: 在.eslintrc.js文件中加上: rules: { // allow async-await 'generator-star-spacing': 'off', // allow ...
- MAC 下用 Common Lisp 调试 OpenGL 程序
MAC 下用 Common Lisp 调试 OpenGL 程序 环境搭建 运行环境: OSX 10.11.3 EI Capitan Common Lisp: SBCL 使用 SBCL, 首先要安装这几 ...
- SQL Server 2008 R2 企业版安装教程
1 安装包解压 2 解压后,打开setup.exe文件,选择安装,显示如图: 3 选择全新安装或向现有安装添加功能 4 点确定 5 输入 企业版序列号:R88PF-GMCFT-KM2KR-4R7GB- ...
- jQuery精仿手机上的翻牌效果菜单
代码简介: jQuery精仿手机上的翻牌效果菜单,很平滑的动画翻牌效果,每点击一下菜单,就会翻去一下,貌似很灵敏的动作.注意:如果预览时没看到效果,请刷新一下页面,让jquery载入就行了,在实际使用 ...
- C++利用cin输入时检测回车的方法
今天做TJU的OJ ,其中一道题是先读入一个字符串,再读入一个整数,循环往复,直到字符串是空,也就是说回车键结束循环. 但是cin对空格和回车都不敏感,都不影响继续读入数据,所以需要一种新的方式检测回 ...
- jQuery基础之二(操作标签)
一:样式操作 addClass();// 添加指定的CSS类名. removeClass();// 移除指定的CSS类名. hasClass();// 判断样式存不存在 toggleClass();/ ...
- Python练习-基于socket的FTPServer
# 编辑者:闫龙 import socket,json,struct class MySocket: with open("FtpServiceConfig","r&qu ...
- 20165329 Java实验二:面向对象编程
实验内容: 面向对象程序设计-1 实验要求: 提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的截图 实验步骤: 1.按照老师博客的要求新建一个MyUtil项目 在src内新建ja ...