本文主要是介绍@Controller,@ResponseBody,@RestController,@RequestMapping,@GetMapping作用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1,@Controller 处理http请求,作用在类上:
package org.springframework.stereotype;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {String value() default "";
}2,@ResponseBody 返回JSON格式的数据,作用在类上或方法上:
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseBody {
}3,@RestController=@ResponseBody + @Controller ,作用在类上:
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Controller;@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {String value() default "";
}4,@RequestMapping 配置url映射,作用在类上或方法上:
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;@Target({ElementType.METHOD, ElementType.TYPE})
@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 {};
}5, @GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
该注解将HTTP Get 映射到 特定的处理方法上。package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;@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 {};
}
6,@RequestBody 注解则是使用 HttpMessageConverter 将 HTTP 请求正文写入某个对象。多用于post请求(insert数据时)
7,@RequestParam 是从request里面取请求参数,而 @PathVariable 是从一个URI模板里面来取值
关于6、7更多请见:
https://blog.csdn.net/u010002184/article/details/80386322
https://blog.csdn.net/ff906317011/article/details/78552426
https://blog.csdn.net/walkerjong/article/details/7946109
http://www.cnblogs.com/liaojie970/p/8591759.html
https://blog.csdn.net/jiangyu1013/article/details/72627352
这篇关于@Controller,@ResponseBody,@RestController,@RequestMapping,@GetMapping作用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!