SpringBoot Controller中使用@RequestParam获取不到参数的问题

在开发前后端发送Post请求时出现的问题

@RequestMapping(value = "/login.do",method = RequestMethod.POST)
public String login(@RequestParam("id") int id,@RequestParam("name") String name) {
}
         submitForm(){
			 //提交表单
			 this.$http.post('/api/test/login.do', {
				 id: this.login.username,
				 name: this.login.password
			 }).then(result => {
				 var resData=result.data;
			 });
		  }

在这里插入图片描述
控制台显示报错
在将后端修改后,问题解决

@RequestMapping(value = "/login.do",method = RequestMethod.POST)
public String login(@RequestBody User userGet) {
}

在这里插入图片描述
寻找出现此问题的原因
在这里插入图片描述
因为前端发送请求时的content-type为 application/json 类型
而@RequestParam 能接收的content-type为application/x-www-form-urlencoded

@RequestParam和@RequestBody 之间的区别
放上区别链接 springMVC中@RequestParam和@RequestBody之contentType

所以修改前端发送请求时的content-type亦可解决问题

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot使用Session非常简单,只需要在Controller注入HttpSession即可。例如: ``` @Controller public class MyController { @Autowired private HttpSession session; @RequestMapping("/setSession") public String setSession(String key, String value) { session.setAttribute(key, value); return "session set"; } @RequestMapping("/getSession") public String getSession(String key) { return session.getAttribute(key); } } ``` 在上面的代码,我们使用了session.setAttribute()和session.getAttribute()来设置和获取session的值。 需要注意的是,Spring Boot默认使用的是内存的session,如果需要持久化session,需要使用其他的session存储方式,如redis等。 <br>### 回答2: Spring Boot是一款优秀的开发框架,它集成了许多开发过程需要的模块,其包括session模块。Session是Web应用常用的一种跨请求的据保存方式,它可以通过一定的方式来保存用户的登录信息、购物车信息等,从而提升用户体验。 在Spring Boot,session的使用非常简单。我们可以通过在Controller添加注解@SessionAttributes来声明一个控制器类所需要保存到Session的属性名;也可以直接使用HttpServletRequest对象的getSession()方法来获取Session对象进行操作。 例如,我们可以通过以下方式将userId保存到Session: ``` @Controller @SessionAttributes("userId") public class UserController { @RequestMapping("/login") public String login(@RequestParam("userId") String userId, Model model) { //通过Model保存到Session model.addAttribute("userId", userId); return "redirect:/home"; } @RequestMapping("/home") public String home(Model model) { //通过Model从Session获取 String userId = (String)model.asMap().get("userId"); return "home"; } } ``` 也可以直接在Controller使用HttpServletRequest对象来获取Session对象: ``` @Controller public class UserController { @RequestMapping("/login") public String login(@RequestParam("userId") String userId, HttpServletRequest request) { //通过HttpServletRequest获取Session对象并保存 HttpSession session = request.getSession(); session.setAttribute("userId", userId); return "redirect:/home"; } @RequestMapping("/home") public String home(HttpServletRequest request) { //通过HttpServletRequest获取Session对象并获取HttpSession session = request.getSession(); String userId = (String)session.getAttribute("userId"); return "home"; } } ``` 需要注意的是,Spring Boot默认使用的Session管理器是Tomcat,这意味着当应用程序被部署到负载平衡的多实例环境时,Session会被锁定在特定的实例上,无法共享。如果需要解决这个问题,可以考虑使用Redis或其他外部存储来管理Session。 <br>### 回答3: Session是一种在Web应用用于跟踪用户状态的机制,它通过在服务器和客户端之间存储用户信息来实现。在SpringBoot,Session可以轻松地集成到应用程序,以便实现各种功能。下面是一些关于SpringBootSession使用的详细介绍: 1. 如何使用Session 在SpringBoot使用Session很简单,只需要在控制器注入HttpServletRequest对象即可。 ```java @RequestMapping("/login") public String login(HttpServletRequest request, Model model){ HttpSession session = request.getSession(); session.setAttribute("user","user info"); return "index"; } ``` 这里使用request.getSession()方法,它将返回一个HttpSession对象。可以使用这个对象来存储用户信息。 2. 设置Session过期时间 在SpringBoot,可以通过配置来设置Session的过期时间。可以使用以下属性来设置Session的过期时间: ```properties # Session过期时间,单位:秒 server.servlet.session.timeout=30 ``` 这里将Session的过期时间设置为30秒。 3. 从Session获取信息 从Session获取用户信息也非常简单,只需要使用getSession()方法获取HttpSession对象,然后使用getAttribute()方法来获取Session存储的据。 ```java @RequestMapping("/index") public String index(HttpServletRequest request){ HttpSession session = request.getSession(); String user = (String) session.getAttribute("user"); return "hello "+user; } ``` 这里使用了getAttribute()方法,它将返回存储在Session据。 4. 从Session删除信息 如果需要从Session删除某个值,可以使用removeAttribute()方法。 ```java @RequestMapping("/logout") public String logout(HttpServletRequest request){ HttpSession session = request.getSession(); session.removeAttribute("user"); return "login"; } ``` 这里使用了removeAttribute()方法,它将删除Session存储的据。 5. 在Session失效时做一些操作 当Session失效时,可能需要在服务器端做一些处理,比如清除缓存。可以使用HttpSessionBindingListener接口来监听Session失效事件,然后在事件触发时执行某些操作。 ```java public class MySessionListener implements HttpSessionBindingListener { private static int total = 0; @Override public void valueBound(HttpSessionBindingEvent event) { // 用户访问了系统,增加在线人 total++; event.getSession().getServletContext().setAttribute("total", total); } @Override public void valueUnbound(HttpSessionBindingEvent event) { // 用户离开系统,减少在线人 total--; event.getSession().getServletContext().setAttribute("total", total); } } ``` 在上面的代码,MySessionListener实现了HttpSessionBindingListener接口,并且增加了在线人。可以使用@WebListener注解将这个类注册为Listener。 ```java @WebListener public class SessionListenerConfig implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent se) { } @Override public void sessionDestroyed(HttpSessionEvent se) { // Session失效事件 HttpSession session = se.getSession(); MySessionListener myListener = new MySessionListener(); myListener.valueUnbound(new HttpSessionBindingEvent(session, null)); } } ``` 在上面的代码,SessionListenerConfig实现了HttpSessionListener接口,并且在接收到Session失效事件时,调用MySessionListener的valueUnbound()方法。这里使用HttpSessionBindingEvent对象,它将Session对象和为空的attribute值进行了绑定,以触发valueUnbound()方法的执行。 6. 在Session存储对象 在Session存储对象时,需要注意对象是否支持序列化。如果对象不支持序列化,将无法存储在Session。需要将对象转化为字符串或使用其他方式进行存储。以Map为例,将Map转换为字符串进行存储。 ```java @RequestMapping("/save") public String save(HttpServletRequest request){ HttpSession session = request.getSession(); Map<String,Object> map=new HashMap<>(); map.put("name","张三"); map.put("age",19); session.setAttribute("userInfo", JSON.toJSONString(map)); return "index"; } @RequestMapping("/show") public String show(HttpServletRequest request, Model model){ HttpSession session = request.getSession(); String userInfo = (String) session.getAttribute("userInfo"); Map<String,Object> map= JSON.parseObject(userInfo,Map.class); model.addAttribute("map", map); return "show"; } ``` 这里使用了JSON库将Map转换为JSON字符串,再将JSON字符串存储到Session。在读取据时,将JSON字符串转换为Map类型即可。使用JSON库的好处是,可以将所有支持JSON格式的对象存储到Session。 以上是关于SpringBootSession使用的介绍。Session是常用的Web开发技术,对于用户的追踪和状态维护有很大的作用。在使用Session时,需要注意安全性和性能问题,以免出现安全漏洞和性能问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值