zoukankan      html  css  js  c++  java
  • RESTful风格、异常处理、Spring框架

    1.RESTful风格

    1. 什么是RESTful风格?

      • REST是REpressentational State Transfer的缩写,中文翻译为表述性状态转移,REST是一种体系结构,而HTTP是一种包含了REST架构属性的协议,为了便于理解,我们把它的首字母拆分成不同的几个部分:
        • 表述性(REpressentational):REST资源实际上可以用各种形式来进行表述,包括XML、JSON、甚至HTML——最适合资源使用者的任意形式;
        • 状态(State):当时用REST的时候,我们更关注资源的状态而不是对资源采取的行为;
        • 转义(Transfer):REST涉及到转义资源数据,它以某种表述性形式从一个应用转移到另一个应用

      简单的说,REST就是将资源的状态以适合客户端或服务端的形式从服务端转移到客户端(或者反过来)。在REST中,资源通过URL进行识别和定位,然后通过行为(即HTTP方法)来定义REST来完成怎样的功能。

    2. 实例说明

      1. 在平时Web开发中,表单中method属性常用的值是GET和POST,但是实际上,HTTP方法还有PATCH、DELETE、PUT等其他值,这些方法通常会匹配如下的CRUD动作:

        CRUD动作 HTTP方法
        Create(增) POST
        Select(查) GET
        Update(改) PUT或PATCH
        Delete(删) DELETE

        在使用RESTful风格之前,我们增加一条商品数据是这样的:
        /addProduct?name=xxx
        但是使用了RESTful风格之后就会变成:

        /product

        这就变成了使用同一个URL,通过约定不同的HTTP方法来实施不同的业务,这就是RESTful风格所做的事

    用户User的实体类

    package com.alibaba.wlq.bean;
    
    public class User {
    	@Override
    	public String toString() {
    		return "User [account=" + account + ", password=" + password + ", phone=" + phone + "]";
    	}
    
    	private String account;
    	private String password;
    	private String phone;
    
    	public String getPhone() {
    		return phone;
    	}
    
    	public void setPhone(String phone) {
    		this.phone = phone;
    	}
    
    	public User(String account, String password, String phone) {
    		super();
    		this.account = account;
    		this.password = password;
    		this.phone = phone;
    	}
    
    	public String getAccount() {
    		return account;
    	}
    
    	public void setAccount(String account) {
    		this.account = account;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public User(String account, String password) {
    		super();
    		this.account = account;
    		this.password = password;
    	}
    
    	public User() {
    		super();
    	}
    	
    }
    

    Controller控制类

    package com.alibaba.wlq.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import com.alibaba.wlq.bean.User;
    @Controller
    @RequestMapping("user")
    public class UserController {
        //RESTful--->user/1
        //method:表示该方法处理get请求
        //1赋值给了{uid}了    
    	@RequestMapping(value="{uid}",method=RequestMethod.GET)//查询操作
    	public String selectById(@PathVariable("uid")int id) {//@PathVariable:把uid的值赋值给形参id
    		System.out.println("id====="+id);
    		return "index";
    	}
    	@RequestMapping(value="{uid}",method=RequestMethod.POST)//添加操作
    	public String addUser(@PathVariable("uid")int id,User user) {
    		System.out.println(user);
    		System.out.println("id====="+id);
    		return "index";
    	}
        //SpringMVC提供了一个过滤器,该过滤器可以把post请求转化为put和delete请求
    	@RequestMapping(method=RequestMethod.PUT)//修改操作
    	@ResponseBody
    	public String updateUser(User user) {
    		System.out.println(user+"======update");
    		return "index";
    	}
    	@RequestMapping(value="{id}",method=RequestMethod.DELETE)//删除操作
    	@ResponseBody
    	public String deleteById(@PathVariable int id) {
    		System.out.println(id+"=====delete");
    		return "index";
    	}
    }
    

    在web.xml配置文件中配置过滤器

    <!-- 把post请求转化为put和delete请求
    	使用_method表示真正的提交方式 -->
    <filter>
    	<filter-name>hiddenHttpMethodFilter</filter-name>
    	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
    	<filter-name>hiddenHttpMethodFilter</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>
    

    index界面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script type="text/javascript" src="/9.5springmvcdemo/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
    	$.ajax({
    		url:"../user",
    		type:"POST",
    		data:{
    			_method:"PUT",
    			"account":"zhangsan",
    			"password":"123456",
    			"phone":"18360917652"
    		},
    		success:function(result){
    			alert(result);
    		}
    	});
    	
    </script>
    </head>
    <body>
    index界面
    </body>
    </html>
    

    2.异常处理

    1. 全局异常处理

      package com.alibaba.wlq.controller;
      import org.springframework.web.bind.annotation.ControllerAdvice;
      import org.springframework.web.bind.annotation.ExceptionHandler;
      import org.springframework.web.servlet.ModelAndView;
      @ControllerAdvice
      public class ExceptionController {
      	@ExceptionHandler
      	public ModelAndView error(Exception exception) {
      		ModelAndView mv = new  ModelAndView();
      		mv.addObject("error",exception.getMessage());
      		mv.setViewName("error");
      		return mv;
      	}
      }
      

    3.Spring框架

    Spring是一个开源框架

    1. 加入jar包

    2. 加入配置文件

  • 相关阅读:
    vue检查用户名是否重复
    后端注册接口完善
    django添加检查用户名和手机号数量接口
    Vue联调,图片及短信验证码
    swift webView 提出这样的要求你能忍吗?
    iOS 如何给Xcode7项目添加“.pch”文件
    swift 定制自己的Button样式
    Swift 为你的webView定制标题
    swift 如何获取webView的内容高度
    如何在MAC上使用SVN,简单几行命令搞定
  • 原文地址:https://www.cnblogs.com/wuliqqq/p/11470321.html
Copyright © 2011-2022 走看看