问题

  1. @RequestMapping(value = { "/lego/mobileMonitor/deviceLogDiagnose.json" }, method = { RequestMethod.POST, RequestMethod.GET })
  2. public void deviceLogDiagnoseTrigger(ModelMap model, @RequestParam(value = "app", required = false) String app,
  3. @RequestParam(value = "platform", required = true) String platform,
  4. @RequestParam(value = "userId", required = true) String userId,
  5. @RequestParam(value = "description", required = false) String description,
  6. @RequestParam(value = "clientTime", required = true) String clientTime)

以上写法只支持get请求,不支持post,虽然写了post,但post不生效

如何写呢

场景方法:https://www.cnblogs.com/xiaoxi/p/5695783.html

一般不建议一个请求既支持post请求,也支持get请求

常见post请求的写法

  1. @RequestMapping(value = "/duplicate.json", method = RequestMethod.POST)
  2. public Object duplicate(@RequestBody JSONObject json) {
  3. PreCheck.isPresent(json, "scName", "scCode", "desc", "condition", "filter", "admins");
  4. String scName = json.getString("scName");
  5. String scCode = json.getString("scCode");
  6. String desc = json.getString("desc");
  7. String condition = json.getString("condition");
  8. }

从参数json中再解析出所需的参数

或通过HttpServletRequest接收(这种写法也支持get)

  1. @RequestMapping("/addUser2")
  2. public String addUser2(HttpServletRequest request) {
  3. String username=request.getParameter("username");
  4. String password=request.getParameter("password");
  5. System.out.println("username is:"+username);
  6. System.out.println("password is:"+password);
  7. return "demo/index";
  8. }

常见get请求的写法

  1. /**
  2. `* 1.直接把表单的参数写在Controller相应的方法的形参中
  3. * @param username
  4. * @param password
  5. * @return
  6. */
  7. @RequestMapping("/addUser1")
  8. public String addUser1(String username,String password) {
  9. System.out.println("username is:"+username);
  10. System.out.println("password is:"+password);
  11. return "demo/index";
  12. }`

注意这种写法默认参数都必传。如果参数不必传,使用@Requestparam注解