Springboot @RequestMapping @RequestBody @ResponseBody
1.get请求
vue发送get请求
const v = this
this.$http.get('http://localhost:8085/index/apply_edit/phone/'+localStorage.getItem('phone'))
.then(function (response) {
})
.catch(function (error) {
console.log(error);
});
springboot响应get请求
@RequestMapping(value="/index/apply_edit/phone/{phone}",method= RequestMethod.GET)
public Candidate getCandidateInfoByPhone(@PathVariable String phone ) {
Candidate candidate = editService.getCandidateInfoByPhone(phone);
return candidate;
}
2.post请求
2.1第一种形式
vue发送post请求,携带数据,字段与实体类中相同
ModifyPersonalInformation:function(){
const _this = this
this.$http.post('http://localhost:8085/index/apply_edit/modify_personal_info', {
id :"",
name:"",
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
springboot响应post请求
Mapper层
@Update("update user set name=#{name} where id=#{id}")
Integer modifyUserInfo(User user);
Controller层
@PostMapping("/index/apply_edit/modify_personal_info")
public Object modifyUserInfo(@RequestBody User user){
return editService.modifyUserInfo(user);
}
2.2第二种形式
vue发送post请求,将数据封装在data中,可以自定义字段
formSubmit() {
const _this = this
this.$http.post('http://localhost:8085/premise/apply_register/signup', {
data:{
phone: this.signup.phone,
password: this.signup.password
}
})
.then(function (response) {
})
.catch(function (error) {
console.log(error);
});
springboot响应post请求
@RequestMapping(value = "/premise/apply_register/signup",method = RequestMethod.POST)
@ResponseBody
public Integer applyRegister(@RequestBody Map<String,Map> para) throws JsonProcessingException {
String phone=para.get("data").get("phone").toString();
String password=para.get("data").get("password").toString();
return login_registerService.applyRegister(phone,password);
}
3.@RequestMapping 详解
配置Spring MVC时,指定请求与处理方法之间的映射关系,可以在控制器类的级别和/或其中的方法的级别上使用
3.1@RequestMapping 来处理多个 URI
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = {
"",
"/page",
"/home",
"/view"
})
String indexMultipleMapping() {
return "Hello from index multiple mapping.";
}
}
3.2 @RequestMapping 中的 @RequestParam
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/name")
String getName(@RequestParam(value = "person", required = false) String personName) {
return "Required element of request param";
}
}
required 定义参数值是否是必须要传的,如果为false,对如下两个请求均会处理
- /home/name?person=xyz
- /home/name
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/name")
String getName(@RequestParam(value = "person", defaultValue = "John") String personName) {
return "Required element of request param";
}
}
如果 person 这个请求参数为空,那么 getName() 处理方法就会接收 John 这个默认值作为其参数。
3.3@RequestMapping 来处理请求参数
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/fetch", params = {
"personId=10"
})
String getParams(@RequestParam("personId") String id) {
return "Fetched parameter using params attribute = " + id;
}
@RequestMapping(value = "/fetch", params = {
"personId=20"
})
String getParamsDifferent(@RequestParam("personId") String id) {
return "Fetched parameter using params attribute = " + id;
}
}
当 URL 是 /home/fetch?id=10 的时候, getParams() 会执行,因为 id 的值是10.
3.4 @RequestMapping 来处理生产和消费对象
@RestController
@RequestMapping("/home")
public class IndexController {
//产生一个 JSON 响应
@RequestMapping(value = "/prod", produces = {
"application/JSON"
})
@ResponseBody
String getProduces() {
return "Produces attribute";
}
//可以同时处理请求中的 JSON 和 XML 内容
@RequestMapping(value = "/cons", consumes = {
"application/JSON",
"application/XML"
})
String getConsumes() {
return "Consumes attribute";
}
}
3.4@RequestMapping 来处理消息头
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/head", headers = {
"content-type=text/plain",
"content-type=text/html"
}) String post() {
return "Mapping applied along with headers";
}
}
3.5@RequestMapping 处理动态 URI @PathVaraible
@RequestMapping("/index/apply_position/city/{city}")
public List<Recruitment> getByCity(@PathVariable("city") String city){
List<Recruitment> slist=recruitmentService.getByCity(city);
return slist;
}
3.6@RequestMapping 默认的处理方法
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping()
String
default () {
return "This is a default method for the class";
}
}
4.@RequestBody
前面提到的一段代码
@PostMapping("/index/apply_edit/modify_personal_info")
public Object modifyUserInfo(@RequestBody User user){
return editService.modifyUserInfo(user);
}
后端@RequestBody注解对应的类在将HTTP的输入流(含请求体)装配到目标类(即:@RequestBody后面 的类)时,会根据json字符串中的key来匹配对应实体类的属性,如果匹配一致且json中的该key对应的值 符合(或可转换为)实体类的对应属性的类型要求时,会调用实体类的setter方法将值赋给该属性。
5.@ResponseBody
@ResponseBody的作用其实是将java对象转为json格式的数据。
注意:在使用 @RequestMapping后,返回值通常解析为跳转路径,但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。 比如异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据。@RequestBody 将 HTTP 请求正文插入方法中,使用适合的 HttpMessageConverter 将请求体写入某个对象。
这里需要解释一下,Controller返回值是ModelAndView对象,对象中可添加model即数据、指定view即试图,数据就是业务所得到的需要的数据,试图为逻辑视图名,通过视图解析器解析为物理视图地址。 所以能理解上述注意中返回结果不会被解析为路径跳转。