URL을 생성할 때 QueryString을 조합하는 문제가 있었다.
적당하게 &와 ?를 버무려야하는데, 딱히 이쁜 생각이 들지 않을 때 아래처럼 apache commons나 spring component를 이용하면 된다.
http://www.leveluplunch.com/java/examples/build-convert-map-to-query-string/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
@Test public void test() throws Exception { UriComponents components = UriComponentsBuilder.fromUriString("http://www.freeism.co.kr/test?id=1000").query("name=freeism").build(); System.out.println(components.toUriString()); UriComponents components2 = UriComponentsBuilder.fromUriString("http://www.freeism.co.kr/test").query("name=freeism").build(); System.out.println(components2.toUriString()); UriComponents components3 = UriComponentsBuilder.fromUriString("http://www.freeism.co.kr/test?id=1000").query( "name=freeism&email=freeism@freeism.co.kr").build(); System.out.println(components3.toUriString()); List<NameValuePair> list = Lists.newArrayList(); list.add(new BasicNameValuePair("name", "freeism")); URIBuilder builder = new URIBuilder("http://www.freeism.co.kr/test?id=1000").addParameters(list); System.out.println(builder.build()); URIBuilder builder2 = new URIBuilder("http://www.freeism.co.kr/test").addParameters(list); System.out.println(builder2.build()); list.add(new BasicNameValuePair("email", "freeism@freeism.co.kr")); URIBuilder builder3 = new URIBuilder("http://www.freeism.co.kr/test?id=1000").addParameters(list); System.out.println(builder3.build()); } |