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