@Controller @ResponseBody @ControllerAdvice @RestController @RestControllerAdvice 注解定义

来源:互联网 发布:回形针淘宝店面设计图 编辑:程序博客网 时间:2024/05/29 15:53

    • Controller
    • ResponseBody
    • RestController
    • ControllerAdvice
    • RestControllerAdvice

@Controller

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Controller {    /**     * The value may indicate a suggestion for a logical component name,     * to be turned into a Spring bean in case of an autodetected component.     * @return the suggested component name, if any     */    String value() default "";}

@ResponseBody

通常是json格式

作用:

该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ResponseBody {}

==========================================

@RestController

@RestController ≈ @Controller + @ResponseBody

@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)@java.lang.annotation.Documented@org.springframework.stereotype.Controller@org.springframework.web.bind.annotation.ResponseBodypublic @interface RestController {    java.lang.String value() default "";}

@ControllerAdvice

@ControllerAdvice 文档

It is typically used to define @ExceptionHandler, @InitBinder, and @ModelAttribute methods that apply to all @RequestMapping methods.

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface ControllerAdvice {    @AliasFor("basePackages")    String[] value() default {};    @AliasFor("value")    String[] basePackages() default {};    Class<?>[] basePackageClasses() default {};    Class<?>[] assignableTypes() default {};    Class<? extends Annotation>[] annotations() default {};}

@RestControllerAdvice

@RestControllerAdvice ≈ @ControllerAdvice + @ResponseBody

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@ControllerAdvice@ResponseBodypublic @interface RestControllerAdvice {    @AliasFor("basePackages")    String[] value() default {};    @AliasFor("value")    String[] basePackages() default {};    Class<?>[] basePackageClasses() default {};    Class<?>[] assignableTypes() default {};    Class<? extends Annotation>[] annotations() default {};}
阅读全文
0 0
原创粉丝点击