一、摘要
注解 | 说明 |
---|---|
@RequestParam | 获取参数 |
@PathVariable | 接收参数,参数值需要在url进行占位 |
@RequestBody | 将 HTTP 请求正文插入方法中 |
@ResponseBody | 将返回结果写入Http响应文中 |
二、详解
1、@RequestParam
- Content-Type必须是application/x-www-form-urlencoded
- 也就是说RequestParam不可以接收json数据(application/json)
- RequestParam有三个属性
- value 请求参数的参数名,作为参数映射名称
- required 该参数是否必填,默认为true(必填),当设置成必填时,如果没有传入参数,报错
- defaultValue 设置请求参数的默认值
前端实现
function updateStudent() {
$("#update-student").bind('click', function () {
if (!confirm("确认更改此配置吗")) {
return;
}
var studentId = $(this).attr("studentId");
var name = $("#student-name").val();
$.ajax({
type: "post",
dataType: "text",
contentType: "application/x-www-form-urlencoded",
traditional: true, //traditional 为true阻止深度序列化
url: "/student/updateStudent",
async: false,
data: {
"studentId": studentId,
"name": name
},
success: function (res) {
notie.alert({
type: 4,
text: res,
stay: false,
time: 3,
position: "top"
})
},
error: function () {
notie.alert({
type: 3,
text: "回调失败",
stay: false,
time: 3,
position: "top"
})
}
})
});
}
关键内容
contentType: “application/x-www-form-urlencoded”
traditional: true, //traditional 为true阻止深度序列化
Controller层
@PostMapping(value = "/updateStudent")
public String updateStudentByStudentId(
@RequestParam(value = "studentId") Integer studentId,
@RequestParam(value = "name") String name) {
try {
studentServerce.updateStudentByStudentId(studentId, name);
} catch (SQLException e) {
return e.toString();
}
return "ok";
}
2、@PathVariable
@RequestMapping("/getUserById/{name}")
public User getUser(@PathVariable("name") String name){
return userService.selectUser(name);
}
若方法参数名和url中一致则可简写
@RequestMapping("/getUser/{name}")
public User getUser(@PathVariable String name){
return userService.selectUser(name);
}
3、@RequestBody
- 将 HTTP 请求正文插入方法中,使用适合的 HttpMessageConverter 将请求体写入某个对象。
- JSON、XML这类数据必须要RequestBody处理
@RequestMapping(value = "user/login")
@ResponseBody
// 将ajax(datas)发出的请求写入 User 对象中
public User login(@RequestBody User user) {
return user;
}
4、@ResponseBody
- 不会再被解析为跳转路径,而是直接将user对象写入 HTTP 响应正文中。 Controller中返回的对象经适当转换器,转换成指定格式,写入到response对象的body区,通常用来返回JSON数据或者是XML
@RequestMapping("/login")
@ResponseBody
public User login(User user){
return user;
}
//User字段:userName pwd
//那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}'
//等同于
@RequestMapping("/login")
public void login(User user, HttpServletResponse response){
response.getWriter.write(JSONObject.fromObject(user).toString());
}
三、附
1、小技巧——模型中属性对应名称
public class User{
@JsonAlias(value = {"Name","name123"})
private String name;
private Integer age;
private String gender;
@JsonProperty("MOTTO")
private String motto;
}
@JsonAlias
- 实现:json转模型时,使json中的特定key能转化为特定的模型属性
- 模型转json时, 对应的转换后的key仍然与属性名一致(即:“name”)
- json字符串转换为模型时,json中key为Name或为name123或为name的都能识别
- JsonAlias注解需要依赖于setter、getter
@JsonProperty
- json转模型时,使json中的特定key能转化为指定的模型属性
- 模型转json时,对应的转换后的key为指定的key(即:“M0TTO”,此时motto不可识别)
- @JsonProperty注解不需要借助setter、getter
注意
- 在不考虑上述两个注解的一般情况下,key与属性匹配时,默认大小写敏感
- 有多个相同的key的json字符串中,转换为模型时,会以相同的几个key中,排在最后的那个key的值给模型属性复制,因为setter会覆盖原来的值。见示例中的gender属性。
参考: