Rest接口
动态页面jsp早已过时,现在流行的是vuejs、angularjs、react等前端框架 调用 rest接口(json格式),如果是单台服务器,用动态还是静态页面可能没什么大区别,如果服务器用到了集群,负载均衡,CDN等技术,用动态页面还是静态页面差别非常大。
传统rest用法
用spring mvc可以很容易的实现json格式的rest接口,这是比较传统的用法,在spring boot中已经自动配置了jackson。
@Controller public class HelloController { @Autowired String hello; @RequestMapping(value = "/hello",method = RequestMethod.POST) @ResponseBody public String hello(User user){ return "hello world"; } }
新的rest用法
在比较新的spring版本中,出了几个新的注解,简化了上面的用法,如下
//@Controller + @ResponseBody 组合 相当于每个方法都加上了@ResponseBody @RestController public class HelloController { @Autowired String hello; //直接指定 Post请求,也有@PutMapping @GetMapping @PostMapping("/hello") public String hello(@RequestBody User user){ //@RequestBody json格式参数->自动转换为user return "hello world"; } }