目录
1.@SpringBootApplication(scanBasepackages="包名")
3.@ConditionalOnBean(name="stringdemo")
5.@ConfigurationProperties(prefix="mycar")
6.@EnableConfigurationProperties(Car.class)
8.@RequestHeader("User-Agent")
1.@SpringBootApplication(scanBasepackages="包名")
作用:放在类上,声明这是一个SpringBoot应用,该类会有一个main方法作为Boot程序的开始。拓展:1.scanBasepackages可以重新定义这个SpringBoot覆盖的范围,默认是本包及其子包
2.该注解=>@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan
@SpringBootApplication()
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext run =
SpringApplication.run(Application.class, args);
}
}
2.@RestController
作用:放在普通类上,声明这是一个web层的类。并且该类中所有方法返回的字符串将作为结果在浏览器上展示,而不是进行页面跳转。
拓展:1.该注解=>@Controller+@ResponseBody
@RestController/*@Controller+@ResponseBody*/
public class helloController {
@Autowired
car car;
@RequestMapping("/hello")
public String handle01(){
return "spring boot的第一次测试";
}
@RequestMapping("/car")
public car handle02(){
return car;
}
}
3.@ConditionalOnBean(name="stringdemo")
作用:放到方法上,如果容器中有id为stringdemo的组件,那将向容器中注入被该注解修饰的组件
@ConditionalOnBean(name="stringdemo")
@Bean
public Pet pet01(){
Pet pet = new Pet("xiaoxiao");
return pet;
}
4.@Bean
作用:放到方法名上,方法名就是组件的id,返回值就是组件的类型class
拓展:多次调用该方法得到的对象是一样的
@Bean
public User user01(){
User user = new User(18, "zhangsan");
return user;
}
5.@ConfigurationProperties(prefix="mycar")
作用:放到组件类上,为类中的属性进行赋值
拓展:属性名字必须要和配置文件中的一致,如果不一致属性值是null。该类必须是容器中已经有
的组件
@Component("car")
@ConfigurationProperties(prefix="mycar")
public class Car {
int money;
String brand;
}
mycar.money=10000 mycar.brand=maibahe
6.@EnableConfigurationProperties(Car.class)
作用:放到配置类上,自动给car类的属性赋值。把Car类自动注入到容器中
拓展:与@ConfigurationProperties(prefix="mycar")注解一起使用
@Configuration
@EnableConfigurationProperties(Car.class)
public class MyConfig {
}
@ConfigurationProperties(prefix="mycar")
public class Car {
int money;
String brand;
}
7.@PathVariable("id")
作用:放在参数里,通过特定路径形式对参数赋值
拓展://localhost:8080/test1/12/zhangsan
@ResponseBody
@RequestMapping("/test1/{id}/{username}") //localhost:8080/test1/12/zhangsan
public Map<String,Object> test1(@PathVariable("id") int id,
@PathVariable("username") String username,
@PathVariable Map<String,String> map){
return null;
}
8.@RequestHeader("User-Agent")
作用:放在参数里,将请求头中的数据封装到参数里
使用://localhost:8080/test2
@RequestMapping("/test2") //localhost:8080/test2
@ResponseBody
public String test2(@RequestHeader("User-Agent") String headString,
@RequestHeader Map<String,String> map){
return map.get("user-agent");
}
9.@RequestParam("name")
作用:放在参数里,通过固定形式来封装参数
使用://localhost:8080/test3?name=xiao&hobbies=basketball&hobbies=games
@RequestMapping("/test3")
@ResponseBody
//localhost:8080/test3?name=xiao&hobbies=basketball&hobbies=games
public String test3(@RequestParam("name") String username,
@RequestParam("hobbies") List<String> hobbies) {
return username;
}
10.@CookieValue("JSESSIONID")
作用:放在参数里,获取secession中的某个属性
使用://localhost:8080/test4
@RequestMapping("/test4")
@ResponseBody
public void save16(@CookieValue("JSESSIONID") String jsessionId,
@CookieValue("JSESSIONID") Cookie cookie){
System.out.println(jsessionId);
}
11.@RequestBody
作用:放在参数里,获取请求体数据
使用:表单提交
@PostMapping("/test5")
public String test5(@RequestBody String content) {
return content;
}
<form action="/test5" method="post"> 名称<input type="text" name="username"><br> <input type="submit" value="确认提交"><br> </form>
12.@RequestAttribute("msg")
作用:放在参数中,获得request域对象中的数据
使用:
@Controller
public class Controller_demo1 {
@RequestMapping("/goto")
public String goTo(HttpServletRequest req){
req.setAttribute("msg","欢迎");
return "forward:/success";
}
@ResponseBody
@RequestMapping("/success")
public String success(@RequestAttribute("msg") String msg,
HttpServletRequest request){
Object msg1 = request.getAttribute("msg");
return msg;
}
}
13.@MatrixVariable("low")
作用:放在参数里,获取矩阵变量的值
使用://localhost:8080/car/sell;low=24;brand=byd,audi,yd
//开放矩阵变量的路径识别
@Configuration
public class MyConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper=new UrlPathHelper();
//设计为不移除;后面的内容,这样矩阵变量就能实现
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
@ResponseBody
@RequestMapping("/car/{path}") //localhost:8080/car/sell;low=24;brand=byd,audi,yd
public Map<String,Object> test1(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List<String> brands,
@PathVariable("path") String path){
Map<String,Object> map=new HashMap<>();
map.put("low",low);
map.put("brand",brands);
map.put("path",path);
return map;
}