SpringMvc源码学习_第二节RequestMethodsRequestCondition、ParamsRequestCondition和HeadersRequestCondition

前面我们简单了解RequestMappingInfo,它包含了7个condition。

1、RequestMethodsRequestCondition

现在我们研究最简单的RequestMethodsRequestCondition。它用于存储@ReqestMaping注解中的method。

RequestMethodsRequestCondition源码:

/**
 * A logical disjunction (' || ') request condition that matches a request
 * against a set of {@link RequestMethod}s.
 *
 * @author Arjen Poutsma
 * @author Rossen Stoyanchev
 * @since 3.1
 */
public final class RequestMethodsRequestCondition extends AbstractRequestCondition<RequestMethodsRequestCondition> {

	private final Set<RequestMethod> methods;


	/**
	 * Create a new instance with the given request methods.
	 * @param requestMethods 0 or more HTTP request methods;
	 * if, 0 the condition will match to every request
	 */
	public RequestMethodsRequestCondition(RequestMethod... requestMethods) {
		this(asList(requestMethods));
	}

	private RequestMethodsRequestCondition(Collection<RequestMethod> requestMethods) {
		this.methods = Collections.unmodifiableSet(new LinkedHashSet<RequestMethod>(requestMethods));
	}


	private static List<RequestMethod> asList(RequestMethod... requestMethods) {
		return (requestMethods != null ? Arrays.asList(requestMethods) : Collections.<RequestMethod>emptyList());
	}


	/**
	 * Returns all {@link RequestMethod}s contained in this condition.
	 */
	public Set<RequestMethod> getMethods() {
		return this.methods;
	}

	@Override
	protected Collection<RequestMethod> getContent() {
		return this.methods;
	}

	@Override
	protected String getToStringInfix() {
		return " || ";
	}

	/**
	 * Returns a new instance with a union of the HTTP request methods
	 * from "this" and the "other" instance.
	 */
	public RequestMethodsRequestCondition combine(RequestMethodsRequestCondition other) {
		Set<RequestMethod> set = new LinkedHashSet<RequestMethod>(this.methods);
		set.addAll(other.methods);
		return new RequestMethodsRequestCondition(set);
	}

	/**
	 * Check if any of the HTTP request methods match the given request and
	 * return an instance that contains the matching HTTP request method only.
	 * @param request the current request
	 * @return the same instance if the condition is empty, a new condition with
	 * the matched request method, or {@code null} if no request methods match
	 */
	public RequestMethodsRequestCondition getMatchingCondition(HttpServletRequest request) {
		if (this.methods.isEmpty()) {
			return this;
		}
		RequestMethod incomingRequestMethod = getRequestMethod(request);
		if(incomingRequestMethod != null) {
			for (RequestMethod method : this.methods) {
				if (method.equals(incomingRequestMethod)) {
					return new RequestMethodsRequestCondition(method);
				}
			}
		}
		return null;
	}

	private RequestMethod getRequestMethod(HttpServletRequest request) {
		try {
			return RequestMethod.valueOf(request.getMethod());
		}
		catch (IllegalArgumentException ex) {
			return null;
		}
	}

	/**
	 * Returns:
	 * <ul>
	 * <li>0 if the two conditions contain the same number of HTTP request methods
	 * <li>Less than 0 if "this" instance has an HTTP request method but "other" doesn't
	 * <li>Greater than 0 "other" has an HTTP request method but "this" doesn't
	 * </ul>
	 * <p>It is assumed that both instances have been obtained via
	 * {@link #getMatchingCondition(HttpServletRequest)} and therefore each instance
	 * contains the matching HTTP request method only or is otherwise empty.
	 */
	public int compareTo(RequestMethodsRequestCondition other, HttpServletRequest request) {
		return (other.methods.size() - this.methods.size());
	}

}

通过源码我们可以发现。@ReqestMaping注解中的method值会被转成一个set集合。




2、ParamsRequestCondition

用于存储@requestMapping中的params属性。String数组类型也会被转成ParamExpression的Set类型。

ParamExpression  实现了NameValueExpression接口。

	public interface NameValueExpression<T> {
        //参数名称
	String getName();
       //参数值
	T getValue();
        //!=为true;=为false。默认为false。
	boolean isNegated();
}

实例:




3、HeadersRequestCondition

用于存储@requestMapping中的headers属性。String数组类型也会被转成HeaderExpression的Set类型。

HeaderExpression和ParamExpression样也  实现了NameValueExpression接口。

实例:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值