问题
@RequestMapping(value = { "/lego/mobileMonitor/deviceLogDiagnose.json" }, method = { RequestMethod.POST, RequestMethod.GET })
public void deviceLogDiagnoseTrigger(ModelMap model, @RequestParam(value = "app", required = false) String app,
@RequestParam(value = "platform", required = true) String platform,
@RequestParam(value = "userId", required = true) String userId,
@RequestParam(value = "description", required = false) String description,
@RequestParam(value = "clientTime", required = true) String clientTime)
以上写法只支持get请求,不支持post,虽然写了post,但post不生效
如何写呢
场景方法:https://www.cnblogs.com/xiaoxi/p/5695783.html
一般不建议一个请求既支持post请求,也支持get请求
常见post请求的写法
@RequestMapping(value = "/duplicate.json", method = RequestMethod.POST)
public Object duplicate(@RequestBody JSONObject json) {
PreCheck.isPresent(json, "scName", "scCode", "desc", "condition", "filter", "admins");
String scName = json.getString("scName");
String scCode = json.getString("scCode");
String desc = json.getString("desc");
String condition = json.getString("condition");
}
从参数json中再解析出所需的参数
或通过HttpServletRequest接收(这种写法也支持get)
@RequestMapping("/addUser2")
public String addUser2(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
常见get请求的写法
/**
`* 1.直接把表单的参数写在Controller相应的方法的形参中
* @param username
* @param password
* @return
*/
@RequestMapping("/addUser1")
public String addUser1(String username,String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}`
注意这种写法默认参数都必传。如果参数不必传,使用@Requestparam注解