目前的主流应用都是前后端分离,我们主要关注于基于REST的控制器。当前例子基于 Spring Boot 2.1.1.RELEASE
。
@Controller
将当前类注册为 Spring MVC
的控制器。
@RestController
将当前类注册为 Spring MVC
的REST控制器,它组合类一个 @ResponseBody
。
@RestController
public class PersonRestController {
}
@RequestMapping
通用的路径到MVC方法的映射,可注解在类上或方法上,支持 GET
POST
PUT
DELETE
PATCH
,我们平常应该使用专用的注解, @RequestMapping
放置在类一级。
@RestController
@RequestMapping("/people")
public class PersonRestController {
}
@PathVariable
可以在请求路径中获取变量。
@GetMapping("/{id}")
public void get(@PathVariable Long id){
log.info("--------" + id + "--------");
}
访问路径为: http://localhost/people/1
@RequestParam
获取请求参数。
@GetMapping("/findByName")
public void findByName(@RequestParam String name){
log.info("--------" + name + "--------");
}
访问路径为: http://localhost/people/findByName?name=wyf
@RequestHeader
获取 请求头
的信息。
@GetMapping("/headerInfo")
public void header(@RequestHeader("User-Agent") String userAgent){
log.info("--------" + userAgent + "--------");
}
@CookieValue
获取客户端的cookie中的信息。
@GetMapping("/getCookieValue")
public void getCookieValue(@CookieValue("tz") String timeZone){
log.info("--------" + timeZone + "--------");
}
@RequestBody
从 请求体
中获取数据。
@PostMapping
public void save(@RequestBody Person person){
log.info("--------" + person + "--------");
}
@ResponseBody
将返回数据写入 返回体
中, @RestController
已包含。
@GetMapping
映射 Http
的 GET
方法,获取信息。
@GetMapping("/{id}")
public void get(@PathVariable Long id){}
@PostMapping
映射 Http
的 POST
方法,保存信息。
PostMapping
public void save(@RequestBody Person person){}
@PutMapping
映射 Http
的 PUT
方法,更新信息。
@PutMapping("/{id}")
public void update(@PathVariable Long id , @RequestBody Person person){}
@DeleteMapping
映射 Http
的 DELETE
方法,删除信息。
@DeleteMapping("/{id}")
public void remove(@PathVariable Long id){}
@PatchMapping
映射 Http
的 PATCH
方法,更新部分信息。
@PatchMapping
public void patch(@PathVariable Long id , @RequestBody Person person){}
@ControllerAdvice
用来处理跨控制器的统一处理,如:异常处理( ExceptionHandler
),数据转换( @InitBinder
)等,它组合了一个 @Component
注解,所以不用再手工注册bean。
@ControllerAdvice
public class WiselyControllerAdvice {}
@ExceptionHandler
针对控制器的全局异常处理。
@ControllerAdvice
public class WiselyControllerAdvice {
@ExceptionHandler(Exception.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String defaultExceptionHandler(HttpServletRequest request, Exception exception) {
return "服务器异常";
}
@ExceptionHandler(IOException.class)
public ResponseEntity<String> IOExceptionnHandler(HttpServletRequest request, Exception exception) {
return new ResponseEntity<String>("服务器IO异常",HttpStatus.INTERNAL_SERVER_ERROR);
}
}
控制器中的演示:
@GetMapping("/exception")
public void exception() throws Exception {
throw new Exception();
}
@GetMapping("/ioException")
public void ioException() throws IOException {
throw new IOException();
}
@InitBinder
将制定格式的字符串专程对象,如将 2-wyf
(id-name),转换成dog对象。
@ControllerAdvice
public class WiselyControllerAdvice {
@InitBinder
public void dataBinding(WebDataBinder binder) {
binder.registerCustomEditor(Dog.class, new DogPropertiesEditor());
}
}
自定义的属性编辑器:
public class DogPropertiesEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] array = text.split("-");
Long id = new Long(array[0]);
String name = array[1];
setValue(new Dog(id , name));
}
}
演示控制器:
@GetMapping("/binder/{dog-txt}")
public void binder(@PathVariable("dog-txt") Dog dog) {
log.info("--------" + dog + "--------");
}
访问地址: http://localhost/binder/2-wyf
@ResponseStatus
定制Http的Reponse的状态:
@GetMapping("/showHttpStatus")
@ResponseStatus(HttpStatus.ALREADY_REPORTED)
public void showHttpStatus(){}
@CrossOrigin
在不同的域下的网页用脚本调用接口的时候存在这跨域的问题,可以用当前注解来允许可以跨域的域。
@GetMapping("/{id}/cross")
@CrossOrigin({"*"})
public void crossGet(@PathVariable Long id){}
@RequestPart
从参数重获取上传的文件:
@PostMapping("/upload")
public void upload(@RequestPart("file") MultipartFile image){
log.info("--------" + image.getOriginalFilename() + "--------");
}