HTTP请求字符串解析

来源:互联网 发布:李升平知乎 编辑:程序博客网 时间:2024/06/07 11:27

开发物流项目,编写接口的时候,遇到了这样的一个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 (filter.contains("PhoneNo eq")) {int b = filter.indexOf("PhoneNo eq");String tempString = filter.substring(b);PhoneNo = tempString.substring(12, 23);System.out.println(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 (filter.contains("PhoneNo eq")) {//截取为PhoneNo eq '10000000000001') and (Execution eq '0-2' or Execution eq '0-3')tempString = filter.substring(filter.indexOf("PhoneNo eq"));//截取为10000000000001') and (Execution eq '0-2' or Execution eq '0-3')//tempString = tempString.substring(tempString.indexOf("'")+1);tempString = tempString.replaceFirst("PhoneNo eq '", "");//截取为10000000000001PhoneNo = tempString.substring(0,tempString.indexOf("'"));System.out.println(PhoneNo);}if (filter.contains("Execution eq")) {tempString = filter.substring(filter.indexOf("Execution eq"));tempString = tempString.substring(tempString.indexOf("'")+1);Execution = tempString.substring(0,tempString.indexOf("'"));System.out.println(Execution);}if (filter.contains("SortFlag eq")) {tempString = filter.substring(filter.indexOf("SortFlag eq"));tempString = tempString.substring(tempString.indexOf("'")+1);SortFlag = tempString.substring(0,tempString.indexOf("'"));System.out.println(SortFlag);}if (filter.contains("CreatedOn ge")) {tempString = filter.substring(filter.indexOf("CreatedOn ge"));tempString = tempString.substring(tempString.indexOf("'")+1);startDateString = tempString.substring(0,tempString.indexOf("'"));System.out.println(startDateString);startDateString = startDateString.replace("T", " ");}if (filter.contains("CreatedOn le")) {tempString = filter.substring(filter.indexOf("CreatedOn le"));tempString = tempString.substring(tempString.indexOf("'")+1);endDateString = tempString.substring(0,tempString.indexOf("'"));System.out.println(endDateString);endDateString = endDateString.replace("T", " ");}// 对$ProcessStatus的解析List<String> processStatusList = new ArrayList<String>();while (filter.contains("ProcessStatus eq")) {tempString = filter.substring(filter.indexOf("ProcessStatus eq"));tempString = tempString.substring(tempString.indexOf("'")+1);tempProcessStatus = tempString.substring(0,tempString.indexOf("'"));processStatusList.add(tempProcessStatus);filter = filter.replaceFirst("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 = filter.split(" and ");List<String> processStatusList = new ArrayList<String>();for(String tempString:list ){if (tempString.contains("PhoneNo eq")) {PhoneNo = tempString.trim().replace("(PhoneNo eq '", "").replace("')", "");}if (tempString.contains("Execution eq")) {Execution = tempString.trim().replace("(Execution eq '", "").replace("')", "").substring(0,2);}if (tempString.contains("SortFlag eq")) {SortFlag = tempString.trim().replace("(SortFlag eq '", "").replace("')", "");}if (tempString.contains("CreatedOn ge")) {System.out.println(tempString);startDateString = tempString.trim().replace("(CreatedOn ge datetime'", "").replace("')", "");startDateString = startDateString.replace("T", " ");}if (tempString.contains("CreatedOn le")) {endDateString = tempString.trim().replace("(CreatedOn le datetime'", "").replace("')", "");endDateString = endDateString.replace("T", " ");}while (tempString.contains("ProcessStatus eq")) {System.out.println(tempString);int b = tempString.indexOf("ProcessStatus eq");tempProcessStatus = tempString.substring(b + 18, b + 20);processStatusList.add(tempProcessStatus);tempString = tempString.replaceFirst("ProcessStatus eq", " ");}}

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

package com.moty.test;import java.util.ArrayList;import java.util.Date;import java.util.List;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 (filter.contains("PhoneNo eq")) {int b = filter.indexOf("PhoneNo eq");String tempString = filter.substring(b);PhoneNo = tempString.substring(12, 23);System.out.println(PhoneNo);}String[] list = filter.split(" and ");List<String> processStatusList = new ArrayList<String>();for (String a : list) {if (a.contains("PhoneNo eq")) {PhoneNo = a.trim().replace("(PhoneNo eq '", "").replace("')", "");System.out.println(PhoneNo);}if (a.contains("Execution eq")) {Execution = a.trim().replace("(Execution eq '", "").replace("')", "").substring(0, 3);System.out.println(Execution);}if (a.contains("SortFlag eq")) {SortFlag = a.trim().replace("(SortFlag eq '", "").replace("')", "");System.out.println(SortFlag);}if (a.contains("CreatedOn ge")) {System.out.println(a);startDateString = a.trim().replace("(CreatedOn ge datetime'", "").replace("')", "");startDateString = startDateString.replace("T", " ");System.out.println(startDateString);}if (a.contains("CreatedOn le")) {endDateString = a.trim().replace("(CreatedOn le  datetime'", "").replace("')", "");endDateString = endDateString.replace("T", " ");System.out.println(endDateString);}while (a.contains("ProcessStatus eq")) {System.out.println(a);int b = a.indexOf("ProcessStatus eq");tempProcessStatus = a.substring(b + 18, b + 20);processStatusList.add(tempProcessStatus);a = a.replaceFirst("ProcessStatus eq", " ");}}// System.out.println(processStatusList.toString());// String[] list = filter.split(" and ");// List<String> processStatusList = new ArrayList<String>();/* * for(String tempString:list ){ if (tempString.contains("PhoneNo eq")) * { PhoneNo = tempString.trim().replace("(PhoneNo eq '", * "").replace("')", ""); } if (tempString.contains("Execution eq")) { * Execution = tempString.trim().replace("(Execution eq '", * "").replace("')", "").substring(0,2); } if * (tempString.contains("SortFlag eq")) { SortFlag = * tempString.trim().replace("(SortFlag eq '", "").replace("')", ""); } * if (tempString.contains("CreatedOn ge")) { * System.out.println(tempString); startDateString = * tempString.trim().replace("(CreatedOn ge datetime'", * "").replace("')", ""); startDateString = startDateString.replace("T", * " "); } if (tempString.contains("CreatedOn le")) { endDateString = * tempString.trim().replace("(CreatedOn le datetime'", * "").replace("')", ""); endDateString = endDateString.replace("T", * " "); } while (tempString.contains("ProcessStatus eq")) { * System.out.println(tempString); int b = * tempString.indexOf("ProcessStatus eq"); tempProcessStatus = * tempString.substring(b + 18, b + 20); * processStatusList.add(tempProcessStatus); tempString = * tempString.replaceFirst("ProcessStatus eq", " "); } } */String tempString = null;if (filter.contains("PhoneNo eq")) {// 截取为PhoneNo eq '10000000000001') and (Execution eq '0-2' or// Execution eq '0-3')tempString = filter.substring(filter.indexOf("PhoneNo eq"));// 截取为10000000000001') and (Execution eq '0-2' or Execution eq// '0-3')// tempString = tempString.substring(tempString.indexOf("'")+1);tempString = tempString.replaceFirst("PhoneNo eq '", "");// 截取为10000000000001PhoneNo = tempString.substring(0, tempString.indexOf("'"));System.out.println(PhoneNo);}if (filter.contains("Execution eq")) {tempString = filter.substring(filter.indexOf("Execution eq"));tempString = tempString.substring(tempString.indexOf("'") + 1);Execution = tempString.substring(0, tempString.indexOf("'"));System.out.println(Execution);}if (filter.contains("SortFlag eq")) {tempString = filter.substring(filter.indexOf("SortFlag eq"));tempString = tempString.substring(tempString.indexOf("'") + 1);SortFlag = tempString.substring(0, tempString.indexOf("'"));System.out.println(SortFlag);}if (filter.contains("CreatedOn ge")) {tempString = filter.substring(filter.indexOf("CreatedOn ge"));tempString = tempString.substring(tempString.indexOf("'") + 1);startDateString = tempString.substring(0, tempString.indexOf("'"));System.out.println(startDateString);startDateString = startDateString.replace("T", " ");}if (filter.contains("CreatedOn le")) {tempString = filter.substring(filter.indexOf("CreatedOn le"));tempString = tempString.substring(tempString.indexOf("'") + 1);endDateString = tempString.substring(0, tempString.indexOf("'"));System.out.println(endDateString);endDateString = endDateString.replace("T", " ");}// 对$ProcessStatus的解析//List<String> processStatusList = new ArrayList<String>();while (filter.contains("ProcessStatus eq")) {tempString = filter.substring(filter.indexOf("ProcessStatus eq"));tempString = tempString.substring(tempString.indexOf("'") + 1);tempProcessStatus = tempString.substring(0, tempString.indexOf("'"));processStatusList.add(tempProcessStatus);filter = filter.replaceFirst("ProcessStatus eq", " ");}System.out.println(processStatusList.toString());}}