当前位置: 首页 > news >正文

网站程序包括数据库和网页程序网站开发免费视频教程

网站程序包括数据库和网页程序,网站开发免费视频教程,佛山企业建网站,电商网站建设收费1、REST 风格 1.1、REST 简介 REST#xff08;Representational State Transfer#xff09;#xff0c;表现形式状态转换 在开发中#xff0c;它其实指的就是访问网络资源的格式 1.1.1、传统风格资源描述形式 http://localhost/user/getById?id1http://localhost/user…1、REST 风格 1.1、REST 简介 RESTRepresentational State Transfer表现形式状态转换 在开发中它其实指的就是访问网络资源的格式 1.1.1、传统风格资源描述形式 http://localhost/user/getById?id1http://localhost/user/saveUser 1.1.2、REST 风格描述形式 在 REST 风格中一般用 模块名  s 的格式描述资源表示这是一类资源而不是单个资源。 4 个常用的 REST 风格请求行为 http://localhost/users        查询所有用户信息        GET 查询http://localhost/users/1        查询指定用户信息        GET 查询http://localhost/users        添加用户信息        POST 新增http://localhost/users        修改用户信息        PUT 更新http://localhost/users/1        删除用户信息        DELETE 删除 REST 风格下对于同一个路径不同的请求代表对资源不同的操作。也就是说我们只需要一个路径和一个请求方式就可以确定一个访问行为。 根据 REST 风格对资源进行访问称为 RESTful 。 优点 最大在优点就是隐藏资源的访问行为无法通过地址得到对资源是如何操作的 注意REST 风格只是一种风格并不是一种规范可以打破。也就是说我们想怎么写就怎么写但是我们只是希望大家都遵守这种风格方便开发。 1.2、RESTful 入门案例 RESTful 就是指使用 REST 风格开发项目。 1.2.1、设定 http 请求动作 REST 风格下我们对资源的不同操作由路径和请求动作决定这里的路径我们统一为 模块名 s所以我们需要把 Controller 类上面的 RequsetMapping 中指定的该模块资源的统一前缀去掉。 添加请求动作只需要先把原本 Controller 类的方法上 RequsetMapping 注解的参数改为 模块名 s  然后通过 method 属性来给方法添加请求动作。 查询都是 GET、新增是 POST 、修改是 PUT、删除是 DELETE。 RequestMapping(value /users,method RequestMethod.POST)ResponseBodypublic String save(RequestBody User user){System.out.println(user save, name user.getName() , age user.getAge());return {module:user save};} 这里的新增用户请求需要传递用户参数这里我们通过 RequestBody 注解来表示从请求体中获取参数也就是通过 json 来传递。 1.2.2、设定请求路径参数 也就是我们控制器类方法上面的注解 RequsetMapping 中的 value 参数这里需要根据需求决定需不需要带参数比如删除 name 为 zs 的用户那就需要指定 value/users/name 而不只是模块名s。 此外对于带有路径变量的请求参数需要给形参前面添加注解 PathVariable 表示从请求路径中解析出该变量当做形参的值。 RequestMapping(value /users/{id},method RequestMethod.DELETE)ResponseBodypublic String delete(PathVariable Integer id){System.out.println(user deleted ... user id id);return {module: user delete};} 完整代码 Controller public class UserController {RequestMapping(value /users,method RequestMethod.POST)ResponseBodypublic String save(RequestBody User user){System.out.println(user save, name user.getName() , age user.getAge());return {module:user save};}RequestMapping(value /users/{id},method RequestMethod.DELETE)ResponseBodypublic String delete(PathVariable Integer id){System.out.println(user deleted ... user id id);return {module: user delete};}RequestMapping(value /users,method RequestMethod.PUT)ResponseBodypublic String update(RequestBody User user){System.out.println(user deleted ... user user.toString());return {module: user update};}RequestMapping(value /users/{id},method RequestMethod.GET)ResponseBodypublic String getById(PathVariable Integer id){System.out.println(user get ... user id id);return {module: user get};}RequestMapping(value /users,method RequestMethod.GET)ResponseBodypublic String getAll(){System.out.println(user get all ...);return {module: user get all};} } 测试结果 查询所有用户 查询 id 为 1 的用户 新增用户 注意新增用户时我们需要给控制器方法传递一个 User 类型创参数因为需要使用 jackson 把我们的 json 转为 java 对象这里要求该对象必须要有无参构造器不然会报错 删除用户 修改用户   1.2.3、请求参数总结 到现在我们共学习了三种请求参数的传递方法 RequestBodyRequestParam用的比较少PathVariable 1.3、RESTful 快速开发 这一节我们来对上面的代码进行简化开发比如所有的控制器方法的前缀都是 /users 所有的控制器方法上面都有一个 ResponseBody 因为我们不需要返回页面。 1.3.1、统一路径前缀 上一节使用 REST 风格后我们发现所有的请求路径的前缀都是 /users 路径变量所以既然都有一个前缀 /users 那么为什么不可以都踢出来呢 RequestMapping(/users) public class UserController {// ... } 这样对于那些在 REST 风格下路径本来就是 模块名s/users的方法直接就可以直接省去 RequestMapping 中的 value 属性对于请求路径包含路径变量的方法可以省去前缀就像这样 RequestMapping(value /{id},method RequestMethod.DELETE)public String delete(PathVariable Integer id){System.out.println(user deleted ... user id id);return {module: user delete};} 1.3.2、RestController 从上一节我们的代码中可以发现控制器类的方法都带有一个 ResponseBody 这是因为因为我们不需要返回页面。所以这里我们可以直接简化把它提到控制器类上面 Controller ResponseBody RequestMapping(/users) public class UserController {// ... } 但是 SpringMVC 还是觉得不够简化它又提供了一个注解 RestController 它就相当于同时包含了 ResponseBody 和 Controller 两个注解毕竟控制器类上面肯定是由 Controller 注解的。所以以后我们直接这么写就行了 RestController RequestMapping(/users) public class UserController {// ... } 1.3.2、标准请求动作注解 经过上一步的简化我们的 UserController 现在变成了这样 RestController RequestMapping(/users) public class UserController {RequestMapping(method RequestMethod.POST)public String save(RequestBody User user){System.out.println(user save, name user.getName() , age user.getAge());return {module:user save};}RequestMapping(value /{id},method RequestMethod.DELETE)public String delete(PathVariable Integer id){System.out.println(user deleted ... user id id);return {module: user delete};}RequestMapping(method RequestMethod.PUT)public String update(RequestBody User user){System.out.println(user deleted ... user user.toString());return {module: user update};}RequestMapping(value /{id},method RequestMethod.GET)public String getById(PathVariable Integer id){System.out.println(user get ... user id id);return {module: user get};}RequestMapping(method RequestMethod.GET)public String getAll(){System.out.println(user get all ...);return {module: user get all};} } 现在看起来还是很冗余我们可以对这些方法的请求行为进行进一步简化现在这么长的参数太扎眼了现在我们的代码就变成了这样把 value   /{param} 和  method RequestMethpd.X 合并到一个注解中 RestController RequestMapping(/users) public class UserController {PostMappingpublic String save(RequestBody User user){System.out.println(user save, name user.getName() , age user.getAge());return {module:user save};}DeleteMapping(/{id})public String delete(PathVariable Integer id){System.out.println(user deleted ... user id id);return {module: user delete};}PutMappingpublic String update(RequestBody User user){System.out.println(user deleted ... user user.toString());return {module: user update};}GetMapping(/{id})public String getById(PathVariable Integer id){System.out.println(user get ... user id id);return {module: user get};}GetMappingpublic String getAll(){System.out.println(user get all ...);return {module: user get all};} } 显然一下子变得清新脱俗舒服多了。 1.4、基于 RESTful 页面数据交互 实体类 package com.lyh.domain;public class Book {private Integer id;private String type;private String name;private String description;public Book(){}public Book(Integer id, String type, String name, String description) {this.id id;this.type type;this.name name;this.description description;}Overridepublic String toString() {return Book{ id id , type type \ , name name \ , description description \ };}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getType() {return type;}public void setType(String type) {this.type type;}public String getName() {return name;}public void setName(String name) {this.name name;}public String getDescription() {return description;}public void setDescription(String description) {this.description description;} }SpringMVC 配置类 Configuration ComponentScan(com.lyh.controller) // 扫描 EnableWebMvc // 开启 json 转对象的功能等各种格式转换 public class SpringMvcConfig {} Sevlet 容器配置类 public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {protected Class?[] getRootConfigClasses() {return new Class[0];}protected Class?[] getServletConfigClasses() {return new Class[]{SpringMvcConfig.class};}protected String[] getServletMappings() {return new String[]{/};}// 乱码处理Overrideprotected Filter[] getServletFilters() {CharacterEncodingFilter filter new CharacterEncodingFilter();filter.setEncoding(UTF-8);return new Filter[]{filter};} } BookController 控制器类 RestController RequestMapping(/books) public class BookController {PostMappingpublic String save(RequestBody Book book){System.out.println(book save ... book book.toString());return {module:book save};}GetMappingpublic ListBook getAll(){ListBook list new ArrayListBook();list.add(new Book(1,编程,《图解Spark》,大数据技术));list.add(new Book(2,小说,《黄金时代》,王小波著作));return list;} } 测试 导入静态资源后访问 localhost:85/pages/boos.html 发现报错找不到映射No Mapping for GET /pages/books.html 这是因为我们现在的请求都被 SpringMVC 拦截走了其实这些静态资源应该是由 Tomcat 处理的。 1.4.1、放行非 SpringMVC 请求 上面的问题是对于静态资源的访问本该是由 Tomcat 处理但是前面我们在 Servlet 配置类中设置了所有请求都由 SpringMVC 来响应。所以现在我们需要通过添加一个配置类来实现放行静态资源 Configuration public class SpringMvcSupport extends WebMvcConfigurationSupport {Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {// 当访问 /pages/下的资源时,走 /pages 目录下的内容registry.addResourceHandler(/pages/**).addResourceLocations(/pages/);registry.addResourceHandler(/js/**).addResourceLocations(/js/);registry.addResourceHandler(/css/**).addResourceLocations(/css/);registry.addResourceHandler(/plugins/**).addResourceLocations(/plugins/);} } 然后我们需要让 SpringMVC 去加载这个新的配置类 这样就可以访问到了
http://www.pierceye.com/news/497665/

相关文章:

  • 温州哪里有网站优化南通营销网站建设
  • 怎么在网站标头做图标wordpress 远程数据库
  • 厦门做手机网站公司最新常州网页制作招聘
  • 施工企业农民工工资专项检查报告百度seo怎么把关键词优化上去
  • 圆通速递我做网站sydney wordpress
  • 做外汇有哪些正规的网站做网站只用前端知识可以吗
  • 奢侈品购物网站排名微分销手机网站制作
  • 东莞市永铭装饰有限公司优质的seo快速排名优化
  • 大型网站服务器配置西宁网站设计建设
  • 网站怎么加ico网站模板上传到那个目录
  • dede关闭网站屯留做网站哪里好
  • 如何用python做网站脚本语言网络行为管理系统
  • 排名好的徐州网站建设微信里的小程序不见了
  • 常州公司网站建设网站基础建设ppt
  • 电商网站产品模块食品包装设计说明范文
  • WordPress的站内地图看网站的浏览器
  • 国外服装购物网站大全网站域名地址查询
  • 莆田专业网站建设公司价格九一制作厂网站app
  • 外贸网站怎么做会吸引眼球wordpress 律师事务所模板
  • 如何用代码制作网站最便宜网站建设
  • 苏州优化网站公司做编程网站有哪些内容
  • 山东省住房和城乡建设网站创建一个网站多少钱
  • 在北京做网站制作一个月多少钱个人电脑可以做网站服务器
  • 正规网站建设找哪家好做经营网站怎么赚钱吗
  • 网站备案需要的资料做网站怎么开发客户
  • 怎么做网站作业贵阳网站推广优化公司
  • 峨眉山有做网站的电话开发类似wordpress
  • 俄语网站建设wordpress 系统安装教程
  • 定制建站网站建设上海门户网站的亮点
  • 访问网站的原理大学生网站开发工作室总结