当前位置:网站首页>@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)

@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)

2022-07-06 21:09:00 Menon research monk

Preface

These annotations are often confused , Even vague cognition
Today, let's sort out this knowledge point

in general
@GetMapping be equal to @RequestMapping(method = RequestMethod.GET)
@PostMapping be equal to @RequestMapping(method = RequestMethod.POST)

A deeper understanding can be seen as follows

1. The source code parsing

Understand a function function or annotation principle , You need to understand deep-seated code to deepen your understanding

@GetMapping The source code is as follows :

@Target({
    ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
    method = {
    RequestMethod.GET}
)
public @interface GetMapping {
    
    @AliasFor(
        annotation = RequestMapping.class
    )
    String name() default "";

    @AliasFor(
        annotation = RequestMapping.class
    )
    String[] value() default {
    };

       @AliasFor(
        annotation = RequestMapping.class
    )
    String[] path() default {
    };

    @AliasFor(
        annotation = RequestMapping.class
    )
    String[] params() default {
    };

    @AliasFor(
        annotation = RequestMapping.class
    )
    String[] headers() default {
    };

    @AliasFor(
        annotation = RequestMapping.class
    )
    String[] consumes() default {
    };

    @AliasFor(
        annotation = RequestMapping.class
    )
    String[] produces() default {
    };
}

From the above code, you can see that some annotations are quoted above

  • Use @Documented Marking the , It's generating javadoc When you do, you will put @Documented The annotations are shown , But it's useless , Just a logo
  • @Retention The function is to define how long the annotation annotated by it will remain ,RetentionPolicy.RUNTIME Annotations are not only saved to class In file ,jvm load class After the document , There is still
  • @Target Act on a method

For these , I also mentioned in my previous article :java Functional interface @FunctionalInterface A detailed analysis of ( For reference )

The most important note indicates :

@RequestMapping(
    method = {
    RequestMethod.GET}
)

See also @PostMapping Annotation source code

@Target({
    ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
    method = {
    RequestMethod.POST}
)
public @interface PostMapping {
    
    @AliasFor(
        annotation = RequestMapping.class
    )
    String name() default "";

    @AliasFor(
        annotation = RequestMapping.class
    )
    String[] value() default {
    };
	
	// Omit the definition code , Specific as @GetMapping Shown 

}

Annotations are roughly the same definition , The only difference is

@RequestMapping(
    method = {
    RequestMethod.POST}
)

See also @RequestMapping Annotation source code

@Target({
    ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    
    String name() default "";

    @AliasFor("path")
    String[] value() default {
    };

    @AliasFor("value")
    String[] path() default {
    };

    RequestMethod[] method() default {
    };

    String[] params() default {
    };

    String[] headers() default {
    };

    String[] consumes() default {
    };

    String[] produces() default {
    };
}

By defining Mapping And RequestMethod[] method() default {}; To specify method What's the way

2. Practical explanation

The use of annotations is simplified as follows :

@RequestMapping(value=/manongyanjiuseng/{
    id},method= RequestMethod.GET)

For actual combat code, it can be combined Restful
But look at my previous article , More comprehensive ( I'm not going to repeat it here )
RESTFul From getting started to mastering super full parsing ( whole )

3. summary

stay Spring4.3 Version after the introduction of @GetMapping、@PostMapping 、 @PutMapping、@DeleteMapping And so on HTTP Mapping of methods

Specifically, you can use Get obtain , Interactive can be used Post obtain ( Upload 、 Order and update subscription information )

原网站

版权声明
本文为[Menon research monk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061251047223.html

随机推荐