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

相關文章
相關標籤/搜索