首页

关于spring-web通过HttpMethod枚举类定义http8中method方法操作类型源码示例

标签:spring-web,HttpMethod,http方法类型,methodType,enum的values(),enum的name,enum用法,枚举类用例     发布时间:2018-09-04   

一、前言

关于spring-web的包中通过org.springframework.http.HttpMethod枚举类,定义了关于http的8种methodType方法类型(GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE),该示例中使用enums的默认values()获取所有枚举实例的值,再通过httpMethod.name()获取对应属性名称注入关系字典中mappings,详情源码示例。

二、源码示例

package org.springframework.http;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@@b@public enum HttpMethod@b@{@b@  GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE;@b@@b@  private static final Map<String, HttpMethod> mappings;@b@@b@  public static HttpMethod resolve(String method)@b@  {@b@    return ((method != null) ? (HttpMethod)mappings.get(method) : null);@b@  }@b@@b@  public boolean matches(String method)@b@  {@b@    return (this == resolve(method));@b@  }@b@@b@  static{@b@    mappings = new HashMap(8);@b@@b@    HttpMethod[] arrayOfHttpMethod = values();@b@    int i = arrayOfHttpMethod.length; @b@    for (int j = 0; j < i; ++j) {@b@        HttpMethod httpMethod = arrayOfHttpMethod[j];@b@      mappings.put(httpMethod.name(), httpMethod);@b@    }@b@  }@b@}