[关闭]
@RitcheeQinG 2020-03-23T09:00:21.000000Z 字数 2912 阅读 242

Spring MVC 注解

Web后端


使用SpringMVC开发Restful API(1)-常用注解的使用

  1. @Controller // 一般用于类,表明该bean是一个Controller的bean
  2. @ResponseBody // 用于类和方法,表示方法返回的是JSON串或者类里面所有的方法返回的都是JSON串
  3. @RestController // 相当于 @Controller + @ResponseBody
  4. @RequestMapping // 将http的url请求映射到java方法,有参数选项
  5. // path url相对地址,第一个字符必须为“/”,一般必填,value参数的别名。
  6. // method url请求类型,可以为:enum RequestMethod{GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE}中的一个或多个
  7. // consumes:url请求必须的Content-Type类型。
  8. // provides:url请求返回的类型
  9. // 举例:path/value
  10. @RequestMapping(path="/mypath")
  11. @RequestMapping(value="/mypath/mysubpath/myres")
  12. // 举例:method
  13. @RequestMapping(path="/myPath", method={RequestMethod.GET, RequestMethod.POST})
  14. // 举例:consumes
  15. @RequestMapping(path="/myPath", consumes = {"text/plain", "application/*"})
  16. // 举例:produces
  17. @RequestMapping(path="/myPath", produces = "application/json; charset=UTF-8")
  18. @PostMapping // url的post请求,相当于@RequestMapping(method=RequestMethod.POST),但只能用在方法上,不能用在类上,其他参数和RequestMapping用法完全相同。在Restful API中代表添加
  19. @GetMapping // 同PostMapping。在Restful API中代表查找
  20. @PutMapping // 同PostMapping。在Restful API中代表更新
  21. @DeleteMapping // 同PostMapping。在Restful API中代表删除
  22. // 特别的,如果类和方法都加了映射注解,那么实际path就是类的path + 方法的path
  23. // 形如 @GetMapping("/user/{id}") 表示给请求url中的/user/后的片段取了个名为id,这个片段在参数中可以用@PathVariable注解获得到
  24. @RequestMapping(value = "myfirstpath/mysecpath/{resid}", method = RequestMethod.GET)
  25. @CrossOrigin
  26. public JSONObject getResponse(
  27. HttpServletRequest request,
  28. HttpServletResponse response,
  29. @PathVariable String resid) throws Exception {
  30. return new JSONOject("");
  31. }
  32. // 形如 @GetMapping("/user/{id:\\d+}") 表示通过正则表达式来限制URL中请求资源类型,这里表示id只能为数字
  33. @RequestParam // 用于方法的参数,表示映射请求参数到java的参数,它有多个参数选项
  34. // name 表示映射请求的参数名,它会从请求的参数中找到和name指定的参数名的参数的值,然后赋给指定方法中的参数。
  35. // defaultValue 如果请求参数中没有该参数,那么会给指定的方法的参数一个默认值。
  36. // required 布尔值,表示请求参数中必须的有该参数
  37. public UserDto getUserInfo(@RequestParam(name="id") String idNo) // 表示将会从请求的参数中找到key为id的参数并将其值赋给idNo
  38. public JSONObject bindIpAndPort(
  39. HttpServletRequest request,
  40. HttpServletResponse response,
  41. @RequestParam(value = "ipAddress", required = false) String ip,
  42. @RequestParam(value = "port", requeired = false) int port) throws Exception {
  43. return new JSONObject(ip + port);
  44. }
  45. @PathVariable // 用于方法的参数,它会从映射指定的url片段到java的参数中,一般常用的参数选项有:
  46. // name:表明要指定哪一个url片段
  47. // required:url片段中是否必须要有该片段
  48. // 示例:假如方法头部添加了@GetMapping("/user/{id}"),方法的参数中有一个@PathVariable(name="id", required=true)String idNo参数,那么表明请求url中必须得有{id}该片段,且会把该片段赋给idNo参数

关于CrossOrigin

注解 @CrossOrigin
什么是跨域?怎么解决跨域问题?
跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制。
所谓同源是指,域名,协议,端口均相同,不明白没关系,举个栗子:
http://www.123.com/index.html 调用 http://www.123.com/server.PHP (非跨域)
http://www.123.com/index.html 调用 http://www.456.com/server.php (主域名不同:123/456,跨域)
http://abc.123.com/index.html 调用 http://def.123.com/server.php(子域名不同:abc/def,跨域)
http://www.123.com:8080/index.html调用 http://www.123.com:8081/server.php(端口不同:8080/8081,跨域)
http://www.123.com/index.html 调用 https://www.123.com/server.php(协议不同:http/https,跨域)
请注意:localhost和127.0.0.1虽然都指向本机,但也属于跨域。
浏览器执行javascript脚本时,会检查这个脚本属于哪个页面,如果不是同源页面,就不会被执行。
当域名www.abc.com下的js代码去访问www.def.com域名下的资源,就会受到限制。
@CrossOrigin可以处理跨域请求,让你能访问不是一个域的文件。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注