Springboot标签
@Controller
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
使用@Controller
修饰类,定义一个控制器类。可以根据需要返回各种我们所需的数据,比如页面跳转等;
@RestController
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
@AliasFor(
annotation = Controller.class
)
String value() default "";
}
而使用RestController
修饰类,最后返回结果都会被解析成json字符串,适合所有的方法返回值都是json数据,可以大致理解为:
@RestController = @Controller + @ResponseBody
@ResponseBody
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseBody {
}
@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。@ResponseBody 表示直接将该方法的返回结果写入 HTTP响应体中。如果你的控制器是用@Controller,不在方法上加@ResponseBody注释的话会将返回结果解析为跳转路径,但是如果加上的话会返回json数据
@RequestBody
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {
boolean required() default true;
}
@RequestBody 将读取Request请求的body部分数据,利用 HttpMessageConverter 将请求体来匹配合适的类,进而自动封装成对应的对象,再把对象数据绑定到 controller中方法的参数上。
当前端通过GET、POST方式发送请求到controller时, 根据request header Content-Type的值来判断所需要什么注解。
application/x-www-form-urlencoded:
可以选择@RequestParam, @ModelAttribute,@RequestBody;
multipart/form-data:
@RequestBody不能处理这种格式的数据
application/json, application/xml等:
必须使用@RequestBody来处理;
@RequetParam
源码:
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
作用:
@RequestParam:将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)
语法:
语法:@RequestParam(value="参数名",required="true/false",defaultValue="")
value:参数名
required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。
defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值
@RequestMapping
@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 {}; //GET(查)、POST(增)、PUT(改)、DELETE(删)
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
-
在@Target中有两个属性,分别为 ElementType.METHOD 和 ElementType.TYPE ,也就是说 @RequestMapping 可以在方法和类的声明中使用
-
可以看到注解中的属性除了 name() 返回的字符串,其它的方法均返回数组,也就是可以定义多个属性值,例如 value() 和 path() 都可以同时定义多个字符串值来接收多个URL请求
将 @RequestMapping 注解在 login 方法上,而indexController上不添加 @RequestMapping 注解,这时的请求 URL 是相对于 根目录
请求地址:/login
前端可以采用Thymeleaf,jsp ,html等。本人在学习过程中用的是vue,下面页面跳转的部分都是采用了Thymeleaf模板的,而jsp并未测试过。假设在Thymeleaf中有一个静态页面index.html,如果登录成功页面跳转到index.html。那么可以直接可以通过 return “index”; 来返回;如果是index.html并未采用Thymeleaf那么需要加入后缀 return “index.html”;
@Controller
public class indexController {
@RequestMapping("/login")
public String login() {
return "index";//返回到login页面,因为没有加@ResponseBody
}
}
将 @RequestMapping 注解在 indexController 类上,这时类的注解是相对于 根目录,而方法上的是相对于类上的路径
请求地址:/user/login
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/login")
public String login() {
return "index";
}
}
method 主要用来定义get,post,put等请求
//get请求
@RequestMapping(path = "/login", method=RequestMethod.GET)
//相当于
@GetMapping("/login")
//post请求
@RequestMapping(path = "/login", method=RequestMethod.GET)
//相当于
@PostMapping("/login")
由于在 RequestMapping 注解类中 method() 方法返回的是 RequestMethod 数组,所以可以给 method 同时指定多个请求方式
@RequestMapping(path = "/login", method={RequestMethod.POST,RequestMethod.GET})
测试 @RequestMapping 的 params 属性,该属性表示请求参数,也就是追加在URL上的键值对,多个请求参数以&隔开,例如:
/**请求地址 http://localhost/user/login?username=zhangsan&password=123456*/
@Controller
@RequestMapping(path = "/user")
public class indexController {
// 该方法将接收 /user/login 发来的请求
@RequestMapping(path = "/login", params={"username=zhangsan","password=123456"})
public String login() {
return "index";
}
}
如果想将url中的参数获取到可以通过 @PathVariable 将url中的占位符绑定到对应方法的参数中,占位符需要使用{}标识,请求的方法必须为GET
@Controller
@RequestMapping(path = "/user")
public class indexController {
@RequestMapping(value="/{username}", method=RequestMethod.GET)
public String getUserName(@PathVariable("username") String username) {
System.out.println(username);
return "login";
}
}
通过这种方法可以动态获取username的内容