【SpringBoot】部分注解介绍
Controller注解
用来处理Http请求的
用在类上面
RequestMapping注解
用来配置Url的映射
可在类或者方法上
在类上时,该注解的映射作用在该类的所有方法上
需要两个地址都访问该注解的方法时,可以将地址用逗号隔开,例如:
@RequestMapping(value = {"/people","people2"},method = RequestMethod.GET)
public People people() {//返回的是对象的JSON字符串 return people;
}
- 1
- 2
- 3
- 4
另外还有@GetMapping,@PostMapping,@PutMapping等组合注解
@GetMapping(value=”/test”)其实就是相当于
@RequestMapping(value = “/test”,method = RequestMethod.GET)
可以看一下GetMapping注解的实现:
...
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping( method = {RequestMethod.GET}
)
public @interface GetMapping { @AliasFor( annotation = RequestMapping.class ) String name() default ""; ...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
可以看到,GetMapping注解继承了RequestMapping注解,并且对于RequestMapping注解中的method属性有一个GET的赋值
所以囖,一目明了。
RestController注解
Spring4之后新加的注解,相当于@Controller和@ResponseBody注解的配合使用。
可以直接返回json字符串
比如上一篇博客使用的,直接在Controller返回一个类对象,在前台看到的是json字符串
可以看下该注解的源码:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController { String value() default "";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
PathVariable注解
获取url中的参数。
例如:
@GetMapping(value = "/getId/{id}") public Integer getPeople(@PathVariable(value = "id")Integer ida) { return ida; }
- 1
- 2
- 3
- 4
我特意将变量命名为ida以区分url和注解中的id。
RequestParam注解
获取请求参数的值
例如:
@GetMapping(value = "/getName") public String getPeople(@RequestParam(value = "name")String nameStr) { return nameStr; }
- 1
- 2
- 3
- 4
不做过多解释啦,学过spring和springmvc的朋友早就懂了的。
其实RequestParam注解也可以省略不写的,这个时候,url传进来的参数就需要和变量名对应上了
还可以给参数设置默认值,可以这样使用:
/** * @RequestParam * required-false 表示不是必须传的 * defaultValue 这个就是参数的默认值,但是注意,这里的值必须是字符串 * @param nameStr * @return */ @GetMapping(value = "/say") public String say(@RequestParam(value = "name",required = false,defaultValue = "0") String nameStr) { return nameStr; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
这篇挺简单的,没有什么多的东西,就没啥代码了,我在后面把那个Controller类贴出来。
建议初学的先从前面的该类博客看
package cn.chenhaoxiang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* Created with IntelliJ IDEA.
* User: 陈浩翔.
* Date: 2017/12/25.
* Time: 下午 9:44.
* Explain:
*/
@RestController
public class HelloController { @Autowired private People people; @RequestMapping(value = {"/people","people2"},method = RequestMethod.GET) public People people() {//返回的是对象的JSON字符串 return people; } @GetMapping(value = "/getId/{id}") public Integer getId(@PathVariable(value = "id")Integer ida) { return ida; } @GetMapping(value = "/getName") public String getName(@RequestParam(value = "name")String nameStr) { return nameStr; } @GetMapping(value = "/getNameStr") public String getNameStr(String nameStr) {//url传进来的参数需要和变量名对应上 return nameStr; } /** * @RequestParam * required-false 表示不是必须传的 * defaultValue 这个就是参数的默认值,但是注意,这里的值必须是字符串 * @param nameStr * @return */ @GetMapping(value = "/say") public String say(@RequestParam(value = "name",required = false,defaultValue = "0") String nameStr) { return nameStr; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
本文章由[谙忆]编写, 所有权利保留。
欢迎转载,分享是进步的源泉。
转载请注明出处:http://chenhaoxiang.cn/2017/12/28/2254/
本文源自【谙忆的博客】
文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。
原文链接:chenhx.blog.csdn.net/article/details/79182569
- 点赞
- 收藏
- 关注作者
评论(0)