后台网站手机版视频怎么做,中企动力z邮局登录,深圳店铺设计,龙岗网站seo1. 常用类注解
RestController和Controller是Spring中用于定义控制器的两个类注解.
1.1 RestController
RestController是一个组合类注解,是Controller和ResponseBody两个注解的组合,在使
用 RestController 注解标记的类中#xff0c;每个方法的返回值都会以 JSON 或 XML…1. 常用类注解
RestController和Controller是Spring中用于定义控制器的两个类注解.
1.1 RestController
RestController是一个组合类注解,是Controller和ResponseBody两个注解的组合,在使
用 RestController 注解标记的类中每个方法的返回值都会以 JSON 或 XML等 的形式直接写入 HTTP 响应体中相当于在每个方法上都添加了 ResponseBody注解.
代码演示(访问127.0.0.1:8080/hello/hello1):
RequestMapping(/hello)
RestController
public class HelloController {RequestMapping(/hello1)public String hello() {return hello spring mvc1;}}
返回的结果如下: 我们可以使用fiddler进行抓包: 可以看到返回的类型是text/html,可以把它看成是一个text或者是html.
1.2 Controller
Controller是一个类注解,其主要作用是标记类表示返回的是一个视图(view).这是用于传统的SpringMVC架构中的类注解.
代码演示:
RequestMapping(/response)
Controller
public class ResponseController {RequestMapping(/index)public String index(){return /index.html;}}
//index.html
!doctype html
html langenheadmeta charsetUTF-8meta nameviewportcontentwidthdevice-width, user-scalableno, initial-scale1.0, maximum-scale1.0, minimum-scale1.0meta http-equivX-UA-Compatible contentieedgetitle用户登录首页/title
/headbody登录人: span idloginUser/spanscript src/jquery.min.js/scriptscript/script
/body/html 这样就返回了一个html页面(视图).
总之,RestController和Controller的区别就在于要返回一个视图(页面)还是要返回数据.
1.3 RequestMapping
RequestMapping表示路由映射,即通过RequestMapping注解可以达到设置url的目的.在访问时只有加入RequestMapping映射的部分,才能成功找到这个路径.\
代码演示:
RequestMapping(/hello)
RestController
public class HelloController {RequestMapping(/hello1)public String hello() {return hello spring mvc1;}
}
我们只有启动服务器,再访问127.0.0.1:8080/hello/hello1才能成功接收到后端返回的hello spring mvc1这个数据. 2. 常用方法注解
2.1 RequestMapping
我们刚才介绍了RequestMapping是一个类注解,它其实还是一个方法注解,我们在使用方法时需要在方法上也添加上这个注解.同样表示的是路由映射,影响我们访问的url.我们再来看一下,其中的属性:
Target({ElementType.TYPE, ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
Documented
Mapping
Reflective({ControllerMappingReflectiveProcessor.class})
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 {};
}
public enum RequestMethod {GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE;
}其中我们主要介绍method这个属性,method取值于RequestMethod这个枚举类型,我们使用的最多的就是GET和POST: GetMapping(/hello3)public String hello3() {return hello spring mvc3;}PostMapping(/hello4)public String hello4() {return hello spring mvc4;}
报错信息:Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method GET is not supported]
POST表示只有使用post请求时才允许进行正确的响应,GET表示get请求时才允许进行正确的响应.如果这个参数不写,就表示post和get请求都允许.
2.2 PostMapping
与 RequestMapping(value /hello2,method RequestMethod.POST)的效果类似,只允许post请求.
2.3 GetMapping
与 RequestMapping(value /hello2,method RequestMethod.GET)的效果类似,只允许get请求.
2.4 RequestParam
这个参数主要有两个作用.
在前端给后端传递参数时,需要规定好参数的名称,使其保持一致否则将无法成功传参.例如前端传给我们的用户名称叫做userName,但是后端接收这个参数的名称叫做name,就无法成功接收.但是我们可以使用RequestParam这个注解进行参数的绑定,将userName绑定到name上,这样后端就可以使用name进行操作了.还可以在其中required属性中设置true/false,表示这个参数是否是必须的.
代码演示: RequestMapping(/param6)public String param6(RequestParam(value userName,required false)String name,String password){System.out.println(receive userName:name password: password);return receive name: name password: password;}
另外一个主要作用是用来传递集合.对于一个简单的数组来说,我们直接传递不会出现任何问题,但是如果我们直接传递一个集合,就会发现传过来的集合是空的.这时,就需要RequestParam这个注解来将数组绑定到这个集合上,给集合赋值.
代码演示: RequestMapping(/param8)public String param8(RequestParam(listParam) ListString listParam){System.out.println(receive listParam: listParam);return receive listParam: listParam;}
2.5 RequestBody
RequestBody这个注解主要用来接收对象(传递JSON类型的参数),比如我们需要给后端传递一个UserInfo对象:
public class UserInfo {public String userName;public String password;public int age;Overridepublic String toString() {return UserInfo{ userName userName \ , password password \ , age age };}
}
我们就需要给出userName,password和age三个参数的值,在传递过程中会将这三个参数转变成JSON格式的数据,只有我们加上RequestBody这个注解才能成功接收到这个JSON数据,并把它转换成对象.
代码演示: RequestMapping(/param9)public String param9(RequestBody UserInfo userInfo){System.out.println(receive userInfo: userInfo);return receive userInfo: userInfo;}
2.6 PathVariable
PathVariable这个注解用来从url中获取到参数.比如下述例子,我们要从url中获取到articlName,我们就可以使用这个注解加上一定的格式进行获取: RequestMapping(/param10/{articlName})public String param10(PathVariable(articlName) Integer articlName){System.out.println(receive articlName: articlName);return receive articlName: articlName;}
2.7 RequestPart
PathVariable这个注解用来进行文件的接收.代码演示如下:
RequestMapping(/param12)public String param12(RequestPart MultipartFile file) throws IOException {System.out.println(file.getName());System.out.println(file.getOriginalFilename());System.out.println(file.getContentType());file.transferTo(new File(D:/temp/file1.getOriginalFilename()));return file1.getOriginalFilename();}
2.8 CookieValue
CookieValue这个注解用来根据cookie中的键获取对应的值.代码演示如下: RequestMapping(/getCookie2)public String getCookie2(CookieValue(lzq) String value) {System.out.println(value);System.out.println(get cookies successfully);return get cookies successfully;}
2.9 SessionAttribute
SessionAttribute这个注解用来根据session中的键获取对应的值.代码演示如下: RequestMapping(/getSession3)public String getSession3(SessionAttribute(value name,required false) String name) {System.out.println(name name);System.out.println(get session successfully);return get session successfully;}
2.10 RequestHeader
RequestHeader这个注解用来根据header中的键获取对应的值.代码演示如下: RequestMapping(/getHeader2)public String getHeader2(RequestHeader(User-Agent) String userAgent) {System.out.println(userAgent);System.out.println(get userAgent successfully);return get userAgent successfully;}
2.11 ResponseBody
ResponseBody是RestController的一部分,通常用在当Controller修饰类控制器,而其中某些方法需要返回数据而不是视图时,代码演示如下: ResponseBodyRequestMapping(/returnData)public String returnData() {System.out.println(returnData);return returnData;}