Mạng xã hội
Hoặc copy link
Annotations @Controller and @RestController có ý nghĩa gì? Và sự khác biệt giữa chúng như thế nào? Cùng tìm hiểu cùng Sharecs.net nha.
@Controller đã được giới thiệu trong bản Spring 2.5 với web controller theo kiến trúc MVC. Tiếp sau đó trong Spring 3.0 đã giới thiệu @ResponseBody để trả lại kiểu trả về của phương thức trực tiếp vào nội dung phản hồi HTTP. Chúng ta có thể kết hợp cả hai @Controller và @ResponseBody để nhanh chóng tạo các dịch vụ web RESTful.
Ví dụ:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping(value="/hello1", method= RequestMethod.GET)
@ResponseBody
String hello1() {
return "Hello World 1";
}
@RequestMapping(value="/hello2", method= RequestMethod.GET)
@ResponseBody
String hello2() {
return "Hello World 2";
}
@RequestMapping(value="/hello3", method= RequestMethod.GET)
@ResponseBody
String hello3() {
return "Hello World 3";
}
}Trong @Controller có gì ?
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
@AliasFor(annotation = Component.class)
String value() default "";
}Mình sẽ giải thích từng annotation một:
Như vậy các class được annotated bởi @Controller bản chất là một singleton bean component.
Vì RESTful đang trở nên phổ biến và một class có thể dễ dàng chứa nhiều @ResponseBody phương thức, tạo ra nhiều mã trùng lặp. Vì vậy, Spring 4.0 được giới thiệu @RestController để đơn giản hóa việc tạo các dịch vụ web RESTful.
Xem lại mã nguồn @RestController bản thân nó cũng được chú thích bằng @Controller và @ResponseBody.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
@AliasFor(annotation = Controller.class)
String value() default "";
}Nhìn vào mã nguồn ta thấy @RestController = @Controller + @ResponseBody .
Lưu ý : @RestController không làm gì khác ngoài hoạt động như một phím tắt cho @Controller và @ResponseBody nó giúp nhà phát triển không phải chú thích @ResponseBody trên tất cả các phương thức xử lý RESTful.
Với @RestController, bây giờ chúng ta có thể viết lại @Controller ví dụ trên như thế này:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value="/hello1", method= RequestMethod.GET)
String hello1() {
return "Hello World 1";
}
@RequestMapping(value="/hello2", method= RequestMethod.GET)
String hello2() {
return "Hello World 2";
}
@RequestMapping(value="/hello3", method= RequestMethod.GET)
String hello3() {
return "Hello World 3";
}
}Lúc này code của chúng ta đã ngắn và đẹp hơn rất nhiều đúng không nào.