在严格遵守restfull开发的过程中,发现通过get、post传递参数没问题,但是通过delete和put方法传递的参数,无论是普通参数还是路径参数同接收不到。导出查资料,在stackoverflow上找到了一种解决方法:
加入pom项:
org.apache.httpcomponents
httpclient
4.5.2
org.apache.httpcomponents
httpcore
4.4.4
加入该类,就可以接收相应的delete方法的参数
package com.attendance.Utils;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
import org.apache.http.annotation.NotThreadSafe;
@NotThreadSafe
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";
public String getMethod() {
return METHOD_NAME;
}
public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpDeleteWithBody() {
super();
}
}
具体原理不太清楚,还需要自己多加学习。
但是还有一点,delete和put方法其实可以接收参数,只不过只能接收application/json数据的参数,通过postman可以测试,如果是表单参数,就只能通过上述方法。在实际开发过程中,为了简便,我采用了application/json传参的方法。