小程序POST请求 后台SSM框架 挖坑

调了好久终于搞定了 总结一下

后台代码  为两种 一种封装bean 一种直接传形参 后台框架为SSM框架

	/*
	 * 提交意见
	 * 
	 */
	@RequestMapping(value = "giveAdvice", method = RequestMethod.POST)
	@ResponseBody
	public Integer giveAdvice(@RequestBody Advice advice) {
		System.out.println(advice);
		System.out.println("=========================" + advice.getUserId() + "================" + advice.getAdviceMsg());
		return adviceService.giveAdvice(advice);
	}

	// 提交意见2
	@RequestMapping(value = "giveAdvice2", method = RequestMethod.POST)
	@ResponseBody
	public Integer giveAdvice2(String UserId, String AdviceMsg) {
		// System.out.println(advice);
		System.out.println("=========================" + UserId + "================" + AdviceMsg);
		return adviceService.giveAdvice2(UserId, AdviceMsg);
	}

一、当小程序请求header为 'content-type': 'application/json' 时

      wx.request({
        url: appUrl+'/giveAdvice',
        data: {
          "UserId":app.globalData.userId,
          "AdviceMsg":advice
        },
        header: {
          'content-type': 'application/json'
        },
        method: "POST",
        success(res){
          console.log(res);
        }
      })
    }

结果第一种giveAdvice API LOG信息为

 "=========================null================null”

添加失败

giveAdvice 2 API LOG信息为  "=========================null================null”

添加失败

二、当小程序请求header为 'content-type': 'application/x-www-form-urlencoded'时

第一种 giveAdvice API log信息为 

DEBUG [http-nio-8080-exec-4] - Returning cached instance of singleton bean 'mobileController'
DEBUG [http-nio-8080-exec-4] - Resolving exception from handler [public java.lang.Integer com.smartcharger.controller.MobileC
ontroller.giveAdvice(com.smartcharger.pojo.Advice)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type
 'application/x-www-form-urlencoded;charset=UTF-8' not supported
DEBUG [http-nio-8080-exec-4] - Resolving exception from handler [public java.lang.Integer com.smartcharger.controller.MobileC
ontroller.giveAdvice(com.smartcharger.pojo.Advice)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type
 'application/x-www-form-urlencoded;charset=UTF-8' not supported
DEBUG [http-nio-8080-exec-4] - Resolving exception from handler [public java.lang.Integer com.smartcharger.controller.MobileC
ontroller.giveAdvice(com.smartcharger.pojo.Advice)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type
 'application/x-www-form-urlencoded;charset=UTF-8' not supported
DEBUG [http-nio-8080-exec-4] - Null ModelAndView returned to DispatcherServlet with name 'SmartCharger': assuming HandlerAdap
ter completed request handling
DEBUG [http-nio-8080-exec-4] - Successfully completed request

添加失败


第二种 giveAdvice2 API log信息为

添加成功!!!!!!!

终于成功了!!!!


结论 小程序的POST请求设置

header: {

          'content-type': 'application/x-www-form-urlencoded'

 }

后台API接口 格式为

	@RequestMapping(value = "giveAdvice2", method = RequestMethod.POST)
	@ResponseBody
	public Integer giveAdvice2(String UserId, String AdviceMsg) {
		// System.out.println(advice);
		System.out.println("=========================" + UserId + "================" + AdviceMsg);
		return adviceService.giveAdvice2(UserId, AdviceMsg);
	}

即可接收POST请求信息!!!

调试了2个小时终于有结果了 挖坑!!!


猜你喜欢

转载自blog.csdn.net/mmlik8878/article/details/79999735