spring RestTemplate基本使用與總結

服務端代碼:

@ApiOperation(value = "登陸", notes = "登陸已經加密")
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public Json login(@ApiParam(value = "用戶名或手機號") @RequestParam String loginName,
			@ApiParam(value = "密碼") @RequestParam String password) {
		return setSimpleSuccess(LoginHelper.login(loginName, password));
	}
/**
	 * 獲取實時公交信息
	 * 
	 * @auther yangyang
	 */
	@ApiOperation(value = "實時信息")
	@GetMapping(value = "/realTime")
	public Json getBusRealTimeInfo(@Valid RealTimeQueryModel realTime) {
		return setSimpleSuccess(
				baseStationService.getRealTimeInfo(realTime));
	}

客戶端代碼:

package util;

import java.util.HashMap;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.xiaoleilu.hutool.http.HttpUtil;

public class UrlTest {
	static RestTemplate restTemplate = new RestTemplate();

	public static void main(String[] args) {
		testHttpUtil();
		testRestTemplateWithPost();
		testRestTemplateWithGet();
		testRestTemplateWithExchangeWithPOST();
	}

	public static void testHttpUtil() {
		String url = "http://127.0.0.1:8080/login/login";
		HashMap<String, Object> paramMap = new HashMap<>();
		paramMap.put("loginName", "admin");
		paramMap.put("password", "888888");
		String post = HttpUtil.post(url, paramMap);
		System.out.println("HttpUtil:=====>" + post);
	}


	public static void testRestTemplateWithPost() {
		String url = "http://127.0.0.1:8080/login/login";
		MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
		paramMap.add("loginName", "bj");
		paramMap.add("password", "888888");
		System.err.println(JSON.toJSON(paramMap));
		ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, paramMap, String.class);
		System.out.println("RestTemplate:===post==>" + postForEntity.getBody());
	}

	public static void testRestTemplateWithGet() {
		String url = "http://120.77.82.161/travel/realTime";
		String params = "{'routeid':['30'],'stationid':['13'],'direction':['1'],'userstationno':['1'],'citycode':['340300']}";
		MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
		paramMap = JSON.parseObject(params, LinkedMultiValueMap.class);
		ResponseEntity<String> postForEntity = restTemplate.getForEntity(url, String.class, paramMap);
		System.out.println("RestTemplate====get=====>" + postForEntity.getBody());
	}

	public static void testRestTemplateWithExchangeWithPOST() {
		String url = "http://127.0.0.1:8080/login/login";
		MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
		paramMap.add("loginName", "admin");
		paramMap.add("password", "888888");
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap);
		ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
		System.out.println("RestTemplate====Exchange===POST==>" + responseEntity.getBody());
	}

}

使用總結:

1.RestTemplate使用post傳參數,參數使用Hashmap 時,服務端須要添加@RequestBody註解:java

public Json showUser( @RequestBody User user) {
		
		return setSimpleSuccess(user);
	}

2.不然使用MultiValueMap 如:web

MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
		paramMap.add("loginName", "admin");
		paramMap.add("password", "888888");

3.RestTemplate使用get傳參數使用不支持對象spring

服務端json

@RequestMapping(value = "/testUser2", method = RequestMethod.GET)
	public Json showUser2( User name) {
		return setSimpleSuccess(name);
	}

客戶端:這種傳參訪問是不支持的app

public static void testRestTemplateWithGET() {
		String url = "http://127.0.0.1:8080/user/testUser";
		MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
		paramMap.add("id", "bj");
		ResponseEntity<String> postForEntity = restTemplate.getForEntity(url, String.class, paramMap);
		System.out.println("RestTemplate:===get==>" + postForEntity.getBody());
	}

建議使用以下方式:post

服務端以下ui

@ApiOperation(value = "顯示用戶", httpMethod = "GET", notes = "顯示用戶")
	@RequestMapping(value = "/{id}/showUser", method = RequestMethod.GET)
	public User showUser(@PathVariable String id) {
		User u = userService.selectByPrimaryKey(id);
		return u;
	}

客戶端訪問以下:加密

Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", 2);
		bean = restTemplateBuilder.build().postForObject("http://localhost:8080/rest/update", map, RoncooUserLog.class);

若是必須使用get傳對象url

那就直接在url上傳傳參數,以下:spa

http://120.77.82.161/travel/realTime?routeid=41&stationid=1074&direction=0&userstationno=30&citycode=450200
相關文章
相關標籤/搜索