通过Apache CXF中的参数将请求映射到方法

时间:2014-10-13 16:54:55

标签: java rest cxf

Apache CXF 能否匹配参数请求?

例如在 Spring mvc 中,我们可以在@RequestMapping参数param的帮助下完成此操作

 @RequestMapping(value = "/test", method = RequestMethod.GET, params={"param1"})
 public String method1(){}

 @RequestMapping(value = "/test", method = RequestMethod.GET, params={"param2"})
 public String method2(){}

Apache Cxf有类似的东西吗?

1 个答案:

答案 0 :(得分:0)

据我所知,Apache CXF在Spring MVC中没有这样的东西,但是你有能力在ResourceComparator的帮助下处理这样的事情。

Look this link for additional info

这是我的比较器(从上面的链接简单复制过去)

public class UrlComparator
  extends OperationResourceInfoComparator
  implements ResourceComparator
{
  public UrlComparator()
  {
    super( null, null );
  }

  @Override
  public int compare( ClassResourceInfo classResourceInfo,
    ClassResourceInfo classResourceInfo2, Message message )
  {
    return 0;
  }

  @Override
  public int compare( OperationResourceInfo operationResourceInfo,
    OperationResourceInfo operationResourceInfo2, Message message )
  {
      // Check if CXF can make a decision
      int cxfResult = super.compare(operationResourceInfo, operationResourceInfo2);
      if (cxfResult != 0)
          return cxfResult;

      int op1Counter = getMatchingRate(operationResourceInfo, message);
      int op2Counter = getMatchingRate(operationResourceInfo2, message);

      return op1Counter == op2Counter
              ? 0
              : op1Counter<  op2Counter
              ? 1
              : -1;
  }

    /**
     * This method calculates a number indicating a good or bad match between
     * values provided within the request and expected method parameters. A
     * higher number means a better match.
     *
     * @param operation
     *            The operation to be rated, based on contained parameterInfo
     *            values.
     * @param message
     *            A message containing query from user request
     * @return A positive or negative number, indicating a good match between
     *         query and method
     */
    protected int getMatchingRate(OperationResourceInfo operation, Message message) {

        List<Parameter> params = operation.getParameters();
        if (params == null || params.size() == 0)
            return 0;

        // Get Request QueryParams
        Set<String> qParams = getParams((String) message.get(Message.QUERY_STRING));

        int rate = 0;
        for (Parameter p : params) {
            switch (p.getType()) {
                case QUERY:
                    if (qParams.contains(p.getName()))
                        rate += 2;
                    else if (p.getDefaultValue() == null)
                        rate -= 1;
                    break;
                // optionally support other parameter types such as headers, etc
                // case HEADER:
                //  break;
                default:
                    break;
            }
        }
        return rate;
    }

    /**
     * @param query
     *            URL Query Example: 'key=value&key2=value2'
     * @return A Set of all keys, contained within query.
     */
    protected Set<String>  getParams(String query) {
        Set<String>  params = new HashSet<String>();
        if (query == null || query.length() == 0)
            return params;

        MultivaluedMap<String, String> allQueries =
                JAXRSUtils.getStructuredParams(query, "&", false, false);
        return allQueries.keySet();
    }
}