GetMethod 사용시 [코드1]과같이 URL을 생성하고 실행하면 [코드2]와 같은 예외가 발생한다.
[코드1]
// Create a method instance.
StringBuilder searchURL = new StringBuilder(this.urlRoot + "/term/search.json");
searchURL.append('?').append("dictionaryId").append('=').append(dictionary.getId());
searchURL.append('&').append("queryString").append('=').append(queryString);
searchURL.append('&').append("start").append('=').append(0);
GetMethod httpMethod = new GetMethod(searchURL.toString());
// Create a method instance.
StringBuilder searchURL = new StringBuilder(this.urlRoot + "/term/search.json");
searchURL.append('?').append("dictionaryId").append('=').append(dictionary.getId());
searchURL.append('&').append("queryString").append('=').append(queryString);
searchURL.append('&').append("start").append('=').append(0);
GetMethod httpMethod = new GetMethod(searchURL.toString());
[코드2]
java.lang.IllegalArgumentException: Invalid uri 'http://localhost:8080/bcf/term/search.json?dictionaryId=6&queryString=윤*&start=0': Invalid query
at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222)
at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
at kr.nextree.nexdic.facade.DictionaryFacade.searchTerms(DictionaryFacade.java:175)
at kr.nextree.nexdic.facade.DictionaryFacadeTest.testSearchTerm(DictionaryFacadeTest.java:68)
java.lang.IllegalArgumentException: Invalid uri 'http://localhost:8080/bcf/term/search.json?dictionaryId=6&queryString=윤*&start=0': Invalid query
at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222)
at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
at kr.nextree.nexdic.facade.DictionaryFacade.searchTerms(DictionaryFacade.java:175)
at kr.nextree.nexdic.facade.DictionaryFacadeTest.testSearchTerm(DictionaryFacadeTest.java:68)
이를 해결하기위해서는 [코드3]과 같이 인코딩되어야할 파라메터값은 인코딩을 해준다.
[코드3]
try {
queryString = URLEncoder.encode(queryString, "UTF-8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
queryString = URLEncoder.encode(queryString, "UTF-8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}