Spring中RestTemplate的使用

2021-09-01 12:22:44 字數 4019 閱讀 7660

1:帶引數的get請求

請求url示例:http://localhost:8080/test/sendsms?phone=手機號&msg=簡訊內容

//錯誤使用:

@autowired

private restoperations restoperations;

public void test() throws exception

**伺服器接收的時候你會發現,接收的該請求時沒有引數的**

//正確使用:

public void test() throws exception&msg=";

mapurivariables = new hashmap();

urivariables.put("phone", "151******xx");

urivariables.put("msg", "測試簡訊內容");

string result = restoperations.getforobject(url, string.class, urivariables);

}public void test() throws exception&msg=";

string result = restoperations.getforobject(url, string.class, "151******xx", "測試簡訊內容");

}

2:spring提供的get請求方法

t getforobject(string url, classresponsetype, object... urivariables) throws restclientexception;

t getforobject(string url, classresponsetype, mapurivariables) throws restclientexception;

t getforobject(uri url, classresponsetype) throws restclientexception;

responseentitygetforentity(string url, classresponsetype, object... urivariables) throws restclientexception;

responseentitygetforentity(string url, classresponsetype, mapurivariables) throws restclientexception;

responseentitygetforentity(uri url, classresponsetype) throws restclientexception;

1:帶引數的post請求

帶引數的url示例:

//正確使用:

responseentityresponseentity = resttemplate.postforentity(positionconstants.create_url, requestentity, string.class);

2:spring提供的post方法

t postforobject(string url, object request, classresponsetype, object... urivariables)

throws restclientexception;

t postforobject(string url, object request, classresponsetype, mapurivariables)

throws restclientexception;

t postforobject(uri url, object request, classresponsetype) throws restclientexception;

responseentitypostforentity(string url, object request, classresponsetype, object... urivariables)

throws restclientexception;

`responseentitypostforentity(string url, object request, classresponsetype, mapurivariables)

throws restclientexception;`

responseentitypostforentity(uri url, object request, classresponsetype) throws restclientexception;`

put請求和post請求差不多.

2:spring提供的put方法

void put(string url, object request, object... urivariables) throws restclientexception;

void put(string url, object request, mapurivariables) throws restclientexception;

void put(uri url, object request) throws restclientexception;

1:spring提供的delete方法

void delete(string url, object... urivariables) throws restclientexception;

void delete(string url, mapurivariables) throws restclientexception;

void delete(uri url) throws restclientexception;

Spring複習筆記 Spring中的Bean

注意構造器例項化 最常用 靜態工廠方式例項化 例項工廠方式例項化 singleton 單例 始終使用的同乙個物件 預設 prototype 原型 每次都是乙個新的bean例項 request session globalsession websocket init method屬性 用於指定bean...

Spring中 Transactional的使用

1.在需要事務管理的地方加 transactional註解,transactional 註解可以被應用於介面定義和介面方法 類定義和類的 public 方法上.2.transactional 註解只能應用到 public 可見度的方法上,如果你在 protected private 或者 packa...

Spring中 Transactional失效問題

spring中的宣告式註解 transactional很大程度的方便了開發者進行db資料儲存。但是在一些特殊情況下,可能會造成註解不是按想定的方式生效,這裡說幾種可能造成的幾種情況。這是一種比較簡單不過稍不注意也可能會犯的情況。spring中事務提交還是回滾是根據呼叫的方法是否丟擲異常來決定的,因此...