我有一个Spring restful web服务,它与邮递员(chrome中的插件)配合使用URL(http://localhost:8080/Assignment/rest/words/ {name} / protection),需要1个名为“name”的参数
我也用java编写的httpclient。使用以下代码。
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://localhost:8080/Assignment/rest/words/{name}/protection");
method.setParameter("name", "value");
byte[] responseBody = null;
int statusCode = 0;
try {
// Executing method.
statusCode = client.executeMethod(method);
}catch(Exception e){}
// checking status code
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Reading response body.
responseBody = method.getResponseBody();
在statusCode = client.executeMethod(method);
上显示错误如下
java.lang.IllegalArgumentException: Invalid uri 'http://localhost:8080/Assignment/rest/words/{name}/protection': escaped absolute path not valid org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222) org.apache.commons.httpclient.methods.ExpectContinueMethod.<init>(ExpectContinueMethod.java:93) org.apache.commons.httpclient.methods.EntityEnclosingMethod.<init>(EntityEnclosingMethod.java:119) org.apache.commons.httpclient.methods.PostMethod.<init>(PostMethod.java:106) com.aspose.talib.service.WordService.getDataFromURL(WordService.java:50) com.aspose.talib.service.WordService.postRequestForProtectData(WordService.java:37) com.aspose.talib.web.WordController.init(WordController.java:21) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) javax.el.BeanELResolver.invoke(BeanELResolver.java:183) javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161) org.apache.el.parser.AstValue.getValue(AstValue.java:173) org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184) com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) javax.faces.component.UIOutput.getValue(UIOutput.java:174) com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205) com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355) com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164) javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458) com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134) com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) javax.faces.webapp.FacesServlet.service(FacesServlet.java:659) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
请帮忙
答案 0 :(得分:1)
显然,URI
中不允许使用大括号等字符。你必须对那部分进行编码。
尝试这样的事情:
PostMethod method = new PostMethod(URIUtil.encodePath("http://localhost:8080/Assignment/rest/words/{name}/protection", "UTF-8"));
method.setParameter("name", "value");
URIUtil
来自commons-httpclient-3.1
。
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
答案 1 :(得分:0)
最好先使用URLBuilder,然后将构建的URL分配给contructor参数:
URIBuilder uriBuilder = new URIBuilder();