在控制层中通常要在控制类加上
常用注解
@Controller
@Controller
public class StudentController {
@ResponseBody
返回字符串或者JSON格式的数据,不返回JSP
@RequestMapping("/update")
@ResponseBody
public Object update(Integer id,String name){
@RestController
是@Contorller和@ResponseBody的结合,用于返回字符串和JSON
//@Controller
@RestController//相当于控制层类上加了@Controller+方法上@ReaponseBody
public class StudentController {
@RequestMapping("/student")
//@ResponseBody
public Object student(){
Student student=new Student();
student.setId(1001);
student.setName("ZhangSan");
return student;
}
@RequestMapping
接受请求路径的映射,支持GET和POST
@RequestMapping("/studentDetail")
public Object queryStudentById(Integer id){
若只支持GET
@RequestMapping(value = "/queryStudentById2",method = RequestMethod.GET)
public Object queryStudentById2(){
@GetMapping
相当于RequestMapping只支持GET,如果请求方式不对会报405错误,该注解通常在查询数据的时候使用
//@RequestMapping(value = "/queryStudentById2",method = RequestMethod.GET)
@GetMapping("/querryStudentById2")//相当于上面这句话
public Object queryStudentById2(){
return "Only Get Method";
}
@PostMapping
只支持POST方法
//@RequestMapping(value = "/insert",method = RequestMethod.POST)
@PostMapping("/insert")
public Object insert(){
return "insert success";
}
@DeleteMapping
//@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
@DeleteMapping("/delete")//相当于上一句话
//该注解通常在删除数据的时候使用,删除操作
public Object delete(){
return "delete Student";
}
@PutMapping
//@RequestMapping(value = "/update",method = RequestMethod.PUT)
@PutMapping("/update")//相当于上一句话
//该注解通常在修改数据的时候使用,更新
public Object update(){
return "update student info";
}