restful架构下springmvc controller中delete、put方法传参问题

    在严格遵照restfull开发的过程当中,发现经过get、post传递参数没问题,可是经过delete和put方法传递的参数,不管是普通参数仍是路径参数同接收不到。导出查资料,在stackoverflow上找到了一种解决方法:java

    加入pom项:apache

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.4</version>
        </dependency>

    加入该类,就能够接收相应的delete方法的参数json

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();
    }
}

    具体原理不太清楚,还须要本身多加学习。restful

    可是还有一点,delete和put方法其实能够接收参数,只不过只能接收application/json数据的参数,经过postman能够测试,若是是表单参数,就只能经过上述方法。在实际开发过程当中,为了简便,我采用了application/json传参的方法。app

相关文章
相关标签/搜索