HTTP请求字符串解析

时间:2025-03-20 07:46:04

开发物流项目,编写接口的时候,遇到了这样的一个url需要解析,url如下:

http://***/get?$filter=(CreatedOn ge datetime'2016-07-21T00%3A00%3A00') and (SortFlag eq 'A') and(PhoneNo eq '13486391009') and (Execution eq '02' or Execution eq'03')&$top=1&$skip=0&$expand=NavCustomerPurchaseOrder

如果是使用$链接的字符串,可以直接使用spring的参数方式接收

@ResponseBody
@RequestMapping("/get")
public Map getDriverFreightOrder(@RequestParam(value="$filter",required=true) String filter,
@RequestParam(value="$top",required=false) Integer top,
@RequestParam(value="$skip",required=false) Integer skip,
@RequestParam(value="$TorId",required=false) String torId) {

通过这样解析之后,变量都可以获取对应的值,现在的问题就是对filter=(CreatedOn ge datetime'2016-07-21T00%3A00%3A00') and (SortFlag eq 'A') and(PhoneNo eq '13486391009') and (Execution eq '02' or Execution eq'03')这个字符串进行解析

1、当时太忙了,懒得思考,就用了最原始的方法(事实证明写程序懒不得,要有足够充分的思考,不然后面会浪费更多的时间,还会影响)

(按照位置使用indexOf取值)

		String filter = "(CreatedOn ge datetime'2016-07-21T00%3A00%3A00') and (SortFlag eq 'A-A') and (PhoneNo eq '15717177064') and (Execution eq '0-2' or Execution eq '0-3') and (ProcessStatus eq '20' or ProcessStatus eq '1-0' or ProcessStatus eq '3---0')";
		String PhoneNo = null;
		String Execution = null;
		String SortFlag = null;
		String startDateString = null;
		String endDateString = null;
		String tempProcessStatus = null;

		if (("PhoneNo eq")) {
			int b = ("PhoneNo eq");
			String tempString = (b);
			PhoneNo = (12, 23);
			(PhoneNo);
		}



2、后来接口调用的时候出问题了,因为方法一是按照固定长度取值的,比如说PhoneNo eq '13486391009',一点长度发生变化,比如变成PhoneNo eq '134',长度不一致,接口妥妥滴会报错。后来改进了这个方法,示例如下:通过定位左右单引号来定位字符串,就不会出现长度取值错误的情况了。

filter=(CreatedOn ge datetime'2016-07-21T00%3A00%3A00') and (SortFlag eq 'A') and(PhoneNo eq '13486391009') and (Execution eq '02' or Execution eq'03')
String tempString = null;
if (("PhoneNo eq")) {
//截取为PhoneNo eq '10000000000001') and (Execution eq '0-2' or Execution eq '0-3')
tempString = (("PhoneNo eq"));
//截取为10000000000001') and (Execution eq '0-2' or Execution eq '0-3')
//tempString = (("'")+1);
tempString = ("PhoneNo eq '", "");
//截取为10000000000001
PhoneNo = (0,("'"));
(PhoneNo);

}
if (("Execution eq")) {
tempString = (("Execution eq"));
tempString = (("'")+1);
Execution = (0,("'"));
(Execution);
}
if (("SortFlag eq")) {
tempString = (("SortFlag eq"));
tempString = (("'")+1);
SortFlag = (0,("'"));
(SortFlag);
}
if (("CreatedOn ge")) {
tempString = (("CreatedOn ge"));
tempString = (("'")+1);
startDateString = (0,("'"));
(startDateString);
startDateString = ("T", " ");
}
if (("CreatedOn le")) {
tempString = (("CreatedOn le"));
tempString = (("'")+1);
endDateString = (0,("'"));
(endDateString);
endDateString = ("T", " ");
}


// 对$ProcessStatus的解析
List<String> processStatusList = new ArrayList<String>();
while (("ProcessStatus eq")) {
tempString = (("ProcessStatus eq"));
tempString = (("'")+1);
tempProcessStatus = (0,("'"));
(tempProcessStatus);
filter = ("ProcessStatus eq", " ");
}


3、方法二算是能够满足解决方案,但是结果还是太麻烦了一点,后来同事告知一种方法,使用replace来解决,确实方便了很多。

filter=(CreatedOn ge datetime'2016-07-21T00%3A00%3A00') and (SortFlag eq 'A') and(PhoneNo eq '13486391009') and (Execution eq '02' or Execution eq'03')
String[] list = (" and ");
List<String> processStatusList = new ArrayList<String>();
for(String tempString:list ){
if (("PhoneNo eq")) {
PhoneNo = ().replace("(PhoneNo eq '", "").replace("')", "");
}
if (("Execution eq")) {
Execution = ().replace("(Execution eq '", "").replace("')", "").substring(0,2);
}
if (("SortFlag eq")) {
SortFlag = ().replace("(SortFlag eq '", "").replace("')", "");
}
if (("CreatedOn ge")) {
(tempString);
startDateString = ().replace("(CreatedOn ge datetime'", "").replace("')", "");
startDateString = ("T", " ");
}
if (("CreatedOn le")) {
endDateString = ().replace("(CreatedOn le datetime'", "").replace("')", "");
endDateString = ("T", " ");
}
while (("ProcessStatus eq")) {
(tempString);
int b = ("ProcessStatus eq");
tempProcessStatus = (b + 18, b + 20);
(tempProcessStatus);
tempString = ("ProcessStatus eq", " ");
}
}

以下是完整的代码,可以直接运行测试

package ;

import ;
import ;
import ;

public class Test {
	public static void main(String[] args) {
		String filter = "(CreatedOn ge datetime'2016-07-21T00%3A00%3A00') and (SortFlag eq 'A-A') and (PhoneNo eq '15717177064') and (Execution eq '0-2' or Execution eq '0-3') and (ProcessStatus eq '20' or ProcessStatus eq '1-0' or ProcessStatus eq '3---0')";
		String PhoneNo = null;
		String Execution = null;
		String SortFlag = null;
		String startDateString = null;
		String endDateString = null;
		String tempProcessStatus = null;

		if (("PhoneNo eq")) {
			int b = ("PhoneNo eq");
			String tempString = (b);
			PhoneNo = (12, 23);
			(PhoneNo);
		}

		String[] list = (" and ");
		List<String> processStatusList = new ArrayList<String>();
		for (String a : list) {
			if (("PhoneNo eq")) {
				PhoneNo = ().replace("(PhoneNo eq '", "")
						.replace("')", "");
				(PhoneNo);
			}
			if (("Execution eq")) {
				Execution = ().replace("(Execution eq '", "")
						.replace("')", "").substring(0, 3);
				(Execution);
			}
			if (("SortFlag eq")) {
				SortFlag = ().replace("(SortFlag eq '", "")
						.replace("')", "");
				(SortFlag);
			}
			if (("CreatedOn ge")) {
				(a);
				startDateString = ()
						.replace("(CreatedOn ge datetime'", "")
						.replace("')", "");
				startDateString = ("T", " ");
				(startDateString);
			}
			if (("CreatedOn le")) {
				endDateString = ()
						.replace("(CreatedOn le  datetime'", "")
						.replace("')", "");
				endDateString = ("T", " ");
				(endDateString);
			}
			while (("ProcessStatus eq")) {
				(a);
				int b = ("ProcessStatus eq");
				tempProcessStatus = (b + 18, b + 20);
				(tempProcessStatus);
				a = ("ProcessStatus eq", " ");
			}
		}
		// (());

		// String[] list = (" and ");
		// List<String> processStatusList = new ArrayList<String>();
		/*
		 * for(String tempString:list ){ if (("PhoneNo eq"))
		 * { PhoneNo = ().replace("(PhoneNo eq '",
		 * "").replace("')", ""); } if (("Execution eq")) {
		 * Execution = ().replace("(Execution eq '",
		 * "").replace("')", "").substring(0,2); } if
		 * (("SortFlag eq")) { SortFlag =
		 * ().replace("(SortFlag eq '", "").replace("')", ""); }
		 * if (("CreatedOn ge")) {
		 * (tempString); startDateString =
		 * ().replace("(CreatedOn ge datetime'",
		 * "").replace("')", ""); startDateString = ("T",
		 * " "); } if (("CreatedOn le")) { endDateString =
		 * ().replace("(CreatedOn le datetime'",
		 * "").replace("')", ""); endDateString = ("T",
		 * " "); } while (("ProcessStatus eq")) {
		 * (tempString); int b =
		 * ("ProcessStatus eq"); tempProcessStatus =
		 * (b + 18, b + 20);
		 * (tempProcessStatus); tempString =
		 * ("ProcessStatus eq", " "); } }
		 */

		String tempString = null;
		if (("PhoneNo eq")) {
			// 截取为PhoneNo eq '10000000000001') and (Execution eq '0-2' or
			// Execution eq '0-3')
			tempString = (("PhoneNo eq"));
			// 截取为10000000000001') and (Execution eq '0-2' or Execution eq
			// '0-3')
			// tempString = (("'")+1);
			tempString = ("PhoneNo eq '", "");
			// 截取为10000000000001
			PhoneNo = (0, ("'"));
			(PhoneNo);

		}
		if (("Execution eq")) {
			tempString = (("Execution eq"));
			tempString = (("'") + 1);
			Execution = (0, ("'"));
			(Execution);
		}
		if (("SortFlag eq")) {
			tempString = (("SortFlag eq"));
			tempString = (("'") + 1);
			SortFlag = (0, ("'"));
			(SortFlag);
		}
		if (("CreatedOn ge")) {
			tempString = (("CreatedOn ge"));
			tempString = (("'") + 1);
			startDateString = (0, ("'"));
			(startDateString);
			startDateString = ("T", " ");
		}
		if (("CreatedOn le")) {
			tempString = (("CreatedOn le"));
			tempString = (("'") + 1);
			endDateString = (0, ("'"));
			(endDateString);
			endDateString = ("T", " ");
		}

		// 对$ProcessStatus的解析
		//List<String> processStatusList = new ArrayList<String>();
		while (("ProcessStatus eq")) {
			tempString = (("ProcessStatus eq"));
			tempString = (("'") + 1);
			tempProcessStatus = tempString
					.substring(0, ("'"));
			(tempProcessStatus);
			filter = ("ProcessStatus eq", " ");
		}
		(());
	}

}