springboot中的 一些注解区别
一.@Controller和@RestController注解区别
1. @RestController注解,相当于 @Controller+@ResponseBody两个注解的结合。
@Controller类中的方法可以直接通过返回String跳转到jsp、ftl、html等模版页面。在方法上加@ResponseBody注解,也可以返回实体对象。
2. @RestController类中的所有方法只能返回String、Object、Json等实体对象,不能跳转到模版页面。
@RestController中的方法如果想跳转页面,则用ModelAndView进行封装。
二…@RequestParam和@PathVariable,@RequestBody区别
- @RequestParam适用于@RequestMapping(“/hi?name=?”)
- @PathVariable适用于@RequestMapping(“/hi/{id = 2}”)
- @RequestBody适用于对象
- 总结一下,RequestParam接收的参数是来自requestHeader中,即请求头,通常用于get请求。RequestBody注解接收的参数是来自requestBody中的,即请求体;一般用于处理非Content-Type:application/x-www-form-urlencoded编码格式的数据,比如application/json、application/xml等类型的数据;
springboot/cloud中的一些报错
一.
报错:
org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET’ not supported,405
解决:把postmapping,deletemapping改成Requestmapping
二.
常见报错:
报错:error create bean xxxxxxxx 这种很有可能是你的pom.xml里面的依赖版本不适配导致冲突,某些注解不能正常生效。
三.
springcloud-config报错(使用config-client去访问config-server配置好的gitee的配置文件)
报错:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'foo' in value "${foo}"
解决:pom.xml里面加入了错误的依赖,应该只有starter-config,删掉第一个config-server。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
改动之后:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
四.
报错:java.lang.IllegalArgumentException: Body parameter 0 was null
解决:
SpringMVC方法中的@requestbody注解接收数据时,当参数为null时就报这个错""
1.(@RequestBody(required=false)
2.传参数时限制authSession不能为空