1.请求设置Content-Type:application/json即可
ajax一般默认:Content-Type: application
/x-www-form-urlencoded
;charset=utf-8
2.服务端:
controller
package com.example.demo; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class helloController { @RequestMapping(value="/hello") public String say(){ String str="my spring boot web project hihihi"; System.out.println(str); return str; } @RequestMapping(value="/test") public String testObj(@RequestBody User user){ System.out.println(user.toString()); return user.toString(); } }
实体user
package com.example.demo; public class User { private String userName; private String pwd; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String toString(){ return userName+""+pwd; } }
3.输出