스프링 쿼리스트링 분할 splitQuery()

1 개요[ | ]

스프링 쿼리스트링 분할 splitQuery()
@RestController
public class HomeController {
	static class Util {
		public static Map<String, List<String>> splitQuery(String query_string) {
			final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
			final String[] pairs = query_string.split("&");
			try {
				for (String pair : pairs) {
					final int idx = pair.indexOf("=");
					String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
					if (!query_pairs.containsKey(key)) query_pairs.put(key, new LinkedList<String>());
					final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;
					query_pairs.get(key).add(value);
				}
				return query_pairs;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return null;
		}	
	}
	@RequestMapping(value = "/")
	public String home(HttpServletRequest req) {
		String queryString = req.getQueryString(); 
		System.out.println( Util.splitQuery(queryString) );
		// INPUT - http://localhost/?param1=value1&param2=&param3=value3&param3
		// OUTPUT - {param1=[value1], param2=[null], param3=[value3, null]}
		return "Hello, World!";
	}
}

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}