org.springframework.web.bind.support.WebArgumentResolver类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(13.0k)|赞(0)|评价(0)|浏览(150)

本文整理了Java中org.springframework.web.bind.support.WebArgumentResolver类的一些代码示例,展示了WebArgumentResolver类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebArgumentResolver类的具体详情如下:
包路径:org.springframework.web.bind.support.WebArgumentResolver
类名称:WebArgumentResolver

WebArgumentResolver介绍

[英]SPI for resolving custom arguments for a specific handler method parameter. Typically implemented to detect special parameter types, resolving well-known argument values for them.

A typical implementation could look like as follows:

public class MySpecialArgumentResolver implements WebArgumentResolver { 
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) { 
if (methodParameter.getParameterType().equals(MySpecialArg.class)) { 
return new MySpecialArg("myValue"); 
} 
return UNRESOLVED; 
} 
}

[中]用于解析特定处理程序方法参数的自定义参数的SPI。通常用于检测特殊的参数类型,为它们解析已知的参数值。
典型的实现可能如下所示:

public class MySpecialArgumentResolver implements WebArgumentResolver { 
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) { 
if (methodParameter.getParameterType().equals(MySpecialArg.class)) { 
return new MySpecialArg("myValue"); 
} 
return UNRESOLVED; 
} 
}

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Delegate to the {@link WebArgumentResolver} instance.
 * @throws IllegalStateException if the resolved value is not assignable
 * to the method parameter.
 */
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
    NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
  Class<?> paramType = parameter.getParameterType();
  Object result = this.adaptee.resolveArgument(parameter, webRequest);
  if (result == WebArgumentResolver.UNRESOLVED || !ClassUtils.isAssignableValue(paramType, result)) {
    throw new IllegalStateException(
        "Standard argument type [" + paramType.getName() + "] in method " + parameter.getMethod() +
        "resolved to incompatible value of type [" + (result != null ? result.getClass() : null) +
        "]. Consider declaring the argument type in a less specific fashion.");
  }
  return result;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Actually resolve the value and check the resolved value is not
 * {@link WebArgumentResolver#UNRESOLVED} absorbing _any_ exceptions.
 */
@Override
public boolean supportsParameter(MethodParameter parameter) {
  try {
    NativeWebRequest webRequest = getWebRequest();
    Object result = this.adaptee.resolveArgument(parameter, webRequest);
    if (result == WebArgumentResolver.UNRESOLVED) {
      return false;
    }
    else {
      return ClassUtils.isAssignableValue(parameter.getParameterType(), result);
    }
  }
  catch (Exception ex) {
    // ignore (see class-level doc)
    logger.debug("Error in checking support for parameter [" + parameter + "], message: " + ex.getMessage());
    return false;
  }
}

代码示例来源:origin: org.springframework/spring-web

/**
 * Delegate to the {@link WebArgumentResolver} instance.
 * @throws IllegalStateException if the resolved value is not assignable
 * to the method parameter.
 */
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
    NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
  Class<?> paramType = parameter.getParameterType();
  Object result = this.adaptee.resolveArgument(parameter, webRequest);
  if (result == WebArgumentResolver.UNRESOLVED || !ClassUtils.isAssignableValue(paramType, result)) {
    throw new IllegalStateException(
        "Standard argument type [" + paramType.getName() + "] in method " + parameter.getMethod() +
        "resolved to incompatible value of type [" + (result != null ? result.getClass() : null) +
        "]. Consider declaring the argument type in a less specific fashion.");
  }
  return result;
}

代码示例来源:origin: org.springframework/spring-web

/**
 * Actually resolve the value and check the resolved value is not
 * {@link WebArgumentResolver#UNRESOLVED} absorbing _any_ exceptions.
 */
@Override
public boolean supportsParameter(MethodParameter parameter) {
  try {
    NativeWebRequest webRequest = getWebRequest();
    Object result = this.adaptee.resolveArgument(parameter, webRequest);
    if (result == WebArgumentResolver.UNRESOLVED) {
      return false;
    }
    else {
      return ClassUtils.isAssignableValue(parameter.getParameterType(), result);
    }
  }
  catch (Exception ex) {
    // ignore (see class-level doc)
    logger.debug("Error in checking support for parameter [" + parameter + "], message: " + ex.getMessage());
    return false;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void supportsParameterThrowsException() throws Exception {
  given(adaptee.resolveArgument(parameter, webRequest)).willThrow(new Exception());
  assertFalse("Parameter supported", adapter.supportsParameter(parameter));
  verify(adaptee).resolveArgument(parameter, webRequest);
}

代码示例来源:origin: spring-projects/spring-framework

@Test(expected = Exception.class)
public void resolveArgumentThrowsException() throws Exception {
  given(adaptee.resolveArgument(parameter, webRequest)).willThrow(new Exception());
  adapter.resolveArgument(parameter, null, webRequest, null);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void supportsParameterUnresolved() throws Exception {
  given(adaptee.resolveArgument(parameter, webRequest)).willReturn(WebArgumentResolver.UNRESOLVED);
  assertFalse("Parameter supported", adapter.supportsParameter(parameter));
  verify(adaptee).resolveArgument(parameter, webRequest);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void supportsParameter() throws Exception {
  given(adaptee.resolveArgument(parameter, webRequest)).willReturn(42);
  assertTrue("Parameter not supported", adapter.supportsParameter(parameter));
  verify(adaptee).resolveArgument(parameter, webRequest);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void supportsParameterWrongType() throws Exception {
  given(adaptee.resolveArgument(parameter, webRequest)).willReturn("Foo");
  assertFalse("Parameter supported", adapter.supportsParameter(parameter));
  verify(adaptee).resolveArgument(parameter, webRequest);
}

代码示例来源:origin: spring-projects/spring-framework

@Test(expected = IllegalStateException.class)
public void resolveArgumentUnresolved() throws Exception {
  given(adaptee.resolveArgument(parameter, webRequest)).willReturn(WebArgumentResolver.UNRESOLVED);
  adapter.resolveArgument(parameter, null, webRequest, null);
}

代码示例来源:origin: spring-projects/spring-framework

@Test(expected = IllegalStateException.class)
public void resolveArgumentWrongType() throws Exception {
  given(adaptee.resolveArgument(parameter, webRequest)).willReturn("Foo");
  adapter.resolveArgument(parameter, null, webRequest, null);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void resolveArgument() throws Exception {
  int expected = 42;
  given(adaptee.resolveArgument(parameter, webRequest)).willReturn(expected);
  Object result = adapter.resolveArgument(parameter, null, webRequest, null);
  assertEquals("Invalid result", expected, result);
}

代码示例来源:origin: philwebb/springfaces

public boolean matches(NativeWebRequest request, MethodParameter methodParameter) {
    if (this.webArgumentResolvers != null) {
      for (WebArgumentResolver resolver : this.webArgumentResolvers) {
        try {
          if (resolver.resolveArgument(methodParameter, request) != WebArgumentResolver.UNRESOLVED) {
            return true;
          }
        } catch (Exception e) {
          throw new IllegalStateException(e);
        }
      }
    }
    return false;
  }
}

代码示例来源:origin: org.parancoe/parancoe-web

protected Object resolveCommonArgument(MethodParameter methodParameter,
    NativeWebRequest webRequest)
    throws Exception {
  if (this.customArgumentResolvers != null) {
    for (WebArgumentResolver argumentResolver : this.customArgumentResolvers) {
      Object value = argumentResolver.resolveArgument(methodParameter,
          webRequest);
      if (value != WebArgumentResolver.UNRESOLVED) {
        return value;
      }
    }
  }
  return resolveStandardArgument(methodParameter.getParameterType(),
      webRequest);
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-web

/**
 * Delegate to the {@link WebArgumentResolver} instance.
 * @throws IllegalStateException if the resolved value is not assignable
 * to the method parameter.
 */
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
    NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
  Class<?> paramType = parameter.getParameterType();
  Object result = this.adaptee.resolveArgument(parameter, webRequest);
  if (result == WebArgumentResolver.UNRESOLVED || !ClassUtils.isAssignableValue(paramType, result)) {
    throw new IllegalStateException(
        "Standard argument type [" + paramType.getName() + "] in method " + parameter.getMethod() +
        "resolved to incompatible value of type [" + (result != null ? result.getClass() : null) +
        "]. Consider declaring the argument type in a less specific fashion.");
  }
  return result;
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * Delegate to the {@link WebArgumentResolver} instance.
 * @throws IllegalStateException if the resolved value is not assignable
 * to the method parameter.
 */
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
    NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
  Class<?> paramType = parameter.getParameterType();
  Object result = this.adaptee.resolveArgument(parameter, webRequest);
  if (result == WebArgumentResolver.UNRESOLVED || !ClassUtils.isAssignableValue(paramType, result)) {
    throw new IllegalStateException(
        "Standard argument type [" + paramType.getName() + "] in method " + parameter.getMethod() +
        "resolved to incompatible value of type [" + (result != null ? result.getClass() : null) +
        "]. Consider declaring the argument type in a less specific fashion.");
  }
  return result;
}

代码示例来源:origin: apache/servicemix-bundles

protected Object resolveCommonArgument(MethodParameter methodParameter, NativeWebRequest webRequest)
    throws Exception {
  // Invoke custom argument resolvers if present...
  if (this.customArgumentResolvers != null) {
    for (WebArgumentResolver argumentResolver : this.customArgumentResolvers) {
      Object value = argumentResolver.resolveArgument(methodParameter, webRequest);
      if (value != WebArgumentResolver.UNRESOLVED) {
        return value;
      }
    }
  }
  // Resolution of standard parameter types...
  Class<?> paramType = methodParameter.getParameterType();
  Object value = resolveStandardArgument(paramType, webRequest);
  if (value != WebArgumentResolver.UNRESOLVED && !ClassUtils.isAssignableValue(paramType, value)) {
    throw new IllegalStateException("Standard argument type [" + paramType.getName() +
        "] resolved to incompatible value of type [" + (value != null ? value.getClass() : null) +
        "]. Consider declaring the argument type in a less specific fashion.");
  }
  return value;
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-web

/**
 * Actually resolve the value and check the resolved value is not
 * {@link WebArgumentResolver#UNRESOLVED} absorbing _any_ exceptions.
 */
@Override
public boolean supportsParameter(MethodParameter parameter) {
  try {
    NativeWebRequest webRequest = getWebRequest();
    Object result = this.adaptee.resolveArgument(parameter, webRequest);
    if (result == WebArgumentResolver.UNRESOLVED) {
      return false;
    }
    else {
      return ClassUtils.isAssignableValue(parameter.getParameterType(), result);
    }
  }
  catch (Exception ex) {
    // ignore (see class-level doc)
    logger.debug("Error in checking support for parameter [" + parameter + "], message: " + ex.getMessage());
    return false;
  }
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * Actually resolve the value and check the resolved value is not
 * {@link WebArgumentResolver#UNRESOLVED} absorbing _any_ exceptions.
 */
@Override
public boolean supportsParameter(MethodParameter parameter) {
  try {
    NativeWebRequest webRequest = getWebRequest();
    Object result = this.adaptee.resolveArgument(parameter, webRequest);
    if (result == WebArgumentResolver.UNRESOLVED) {
      return false;
    }
    else {
      return ClassUtils.isAssignableValue(parameter.getParameterType(), result);
    }
  }
  catch (Exception ex) {
    // ignore (see class-level doc)
    logger.debug("Error in checking support for parameter [" + parameter + "], message: " + ex.getMessage());
    return false;
  }
}

代码示例来源:origin: org.springframework/org.springframework.web.portlet

/**
 * Resolves common method arguments. Delegates to registered
 * {@link #setCustomArgumentResolver argumentResolvers} first,
 * then checking {@link #resolveStandardArgument}.
 * @param methodParameter the method parameter
 * @param webRequest the request
 * @param thrownException the exception thrown
 * @return the argument value, or {@link org.springframework.web.bind.support.WebArgumentResolver#UNRESOLVED}
 */
protected Object resolveCommonArgument(MethodParameter methodParameter, NativeWebRequest webRequest,
    Exception thrownException) throws Exception {
  // Invoke custom argument resolvers if present...
  if (this.customArgumentResolvers != null) {
    for (WebArgumentResolver argumentResolver : this.customArgumentResolvers) {
      Object value = argumentResolver.resolveArgument(methodParameter, webRequest);
      if (value != WebArgumentResolver.UNRESOLVED) {
        return value;
      }
    }
  }
  // Resolution of standard parameter types...
  Class<?> paramType = methodParameter.getParameterType();
  Object value = resolveStandardArgument(paramType, webRequest, thrownException);
  if (value != WebArgumentResolver.UNRESOLVED && !ClassUtils.isAssignableValue(paramType, value)) {
    throw new IllegalStateException("Standard argument type [" + paramType.getName() +
        "] resolved to incompatible value of type [" + (value != null ? value.getClass() : null) +
        "]. Consider declaring the argument type in a less specific fashion.");
  }
  return value;
}

相关文章

WebArgumentResolver类方法