使用RestTemplate 上傳檔案

2021-08-22 04:40:13 字數 990 閱讀 6693

public

static

void

main(string args) catch (restclientexception e)

}

請求的controller的**

@responsebody

public

void

sendcommnand(@requestbody jsonobject json)

如果不需要返回值,兩邊可以直接配成 void。

兩邊的引數的結果要相同,否則會報415錯誤,即不支援的型別。

如果服務端有返回值,但是請求端是用void接收,會報錯。(返回值怎麼可能轉化成void)

如果服務端沒有返回值,請求端如果用其他型別接收,都是null。

即便 resttemplate 中的返回值和服務端的返回值不同,也可以請求到,只是客戶端會報異常:org.springframework.web.client.restclientexception: could not extract response: no suitable httpmessageconverter found for response type ……

服務端的引數要加@requestbody的註解

public

static

void

transfile()

@responsebody

public

void

remoteupdtetest(string deviceid,@requestparam multipartfile file)

如果有@requestparam註解,multivaluemap中的名字必須與它對應,否則請求不到。

如果沒有@requestparam註解,multivaluemap中的名字與它不對應,可以請求到,但是file為null。

RestTemplate使用總結

resttemplate的介紹就不說了,總的來說用這個物件可以很方便的模擬乙個http請求。talk is cheap,show me the code,使用的是springboot整合的工程所以以下涉及到的物件都是註解式的宣告和注入,一 宣告resttemplate物件 當然,可以利用註解 bea...

RestTemplate使用總結

resttemplate的介紹就不說了,總的來說用這個物件可以很方便的模擬乙個http請求。talk is cheap,show me the code,使用的是springboot整合的工程所以以下涉及到的物件都是註解式的宣告和注入,一 宣告resttemplate物件 當然,可以利用註解 bea...

使用restTemplate隨筆

使用resttemplate訪問其他介面時 採用post方式傳參 發現服務端接受不到引數 坑了半天 記錄一下 服務端是個servlet 以post接參 所以本地使用mvc模擬了一下 方便除錯 如下 客戶端 如下 test public void test1 不能以map或hashmap代替 否則服務...