spring boot 无法接收小程序 wx.request post请求传递的参数问题

问题描述:最近在做一个小程序,后端用的java。在使用wx.request 来传递参数的过程中发现后端不能成功接收传递的参数。

代码

小程序端代码如下

wx.request({
      url: 'http://localhost:8080/project/phonePassword',
      method:"POST",
      data:{
        "phone":phone,
        "password":password
      },
      header: {
        'content-type': 'application/json'//默认值
      },
      success (res) {
        console.log(res.data)
      }
    })

java控制器代码如下

@PostMapping("/phone/phonePassword")
@ResponseBody
public BuildUser getByUserId(@RequestParam String password,@RequestParam String phone){
    return buildUserService.getByPhonePossword(phone,password);
}

结果发现报错

Required String parameter 'possword' is not present

查阅资料发现问题所在

在http请求中content-type 来确定请求的媒体类型(格式)

1.当content-type的值为application/x-www-form-urlencoded时
浏览器的原生 <form> 表单,其中ajax也是用这种方式提交的
这种情况下可以使用@RequestParam@ModelAttribute处理,当然@RequestBody也能处理

2.当content-type的值为multipart/form-data时
表单上传文件用的这种提交方式
不能处理,使用@RequestBody注解也不能处理。

3.当content-type的值为application/json或者application/xml
这种提交方式的消息主体是一个json字符串         
只能使用@RequestBody注解来处理这种参数。

解决方式

1.第一种解决方式改变小程序wx.request请求头中的content-type类型,值改为application/x-www-form-urlencoded

 wx.request({
      url: 'http://localhost:8080/partybuild/buildUser/phone/phonePossword',
      method:"POST",
      data:{
        "phone":phone,
        "possword":possword
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success (res) {
        console.log(res.data)
      }
    })

2.第二种方式 是 Controller 用@RequestBody 接收参数,小程序还是’content-type’: 'application/json’不变,

@PostMapping("/phone/phonePassword")
@ResponseBody
public BuildUser getByUserId(@RequestBody Map<String, String> req){
return buildUserService.getByPhonePossword(req.get("phone"),req.get("password"));
}

原文链接http://www.jhone.top/index/index/toread.html?article_id=11

微信小程序和后台Spring Boot的连接需要通过RESTful API接口实现。RESTful是一种架构风格,它使用HTTP协议的GET、POST、PUT、DELETE等请求方法来实现对数据的操作,具有简单、灵活、可扩展性强等特点。 在微信小程序中,可以使用wx.request()方法向后台发送HTTP请求请求的URL为后台RESTful API接口的地址,请求的方法为GET、POST、PUT或DELETE等。 在后台Spring Boot中,可以使用Spring MVC框架来实现RESTful API接口。Spring MVC通过注解@RequestMapping来定义RESTful接口,使用@RequestBody注解来接收请求的JSON数据,使用@ResponseBody注解来返回JSON格式的数据。 例如,定义一个获取用户信息的接口,可以在后台Spring Boot中定义如下的UserController类: ``` @RestController @RequestMapping("/user") public class UserController { @GetMapping("/{id}") public User getUser(@PathVariable("id") Long id) { // 根据id查询用户信息 User user = userService.getUserById(id); return user; } @PostMapping public void addUser(@RequestBody User user) { // 添加用户信息 userService.addUser(user); } @PutMapping("/{id}") public void updateUser(@PathVariable("id") Long id, @RequestBody User user) { // 更新用户信息 userService.updateUser(id, user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable("id") Long id) { // 删除用户信息 userService.deleteUser(id); } } ``` 可以看到,上述代码中使用了@GetMapping、@PostMapping、@PutMapping和@DeleteMapping注解来定义RESTful接口,使用@PathVariable注解来获取请求参数,使用@RequestBody注解来接收请求的JSON数据,使用@ResponseBody注解来返回JSON格式的数据。 通过上述代码,就可以在微信小程序中使用wx.request()方法向后台发送HTTP请求请求的URL为"/user/{id}"、"/user"、"/user/{id}"或"/user/{id}",请求的方法为GET、POST、PUT或DELETE等,从而实现微信小程序和后台Spring Boot的连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

残城碎梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值