以下案例基于:Spring 3.2x 版本。
用于测试的Action(controller)。
@RequestMapping(value = "getData.json") public @ResponseBody ServerResponse getData(HttpSession session, String flag){ String str; if(!StringUtils.isBlank(flag)){ str = "您传的是“"+flag+"”"; }else{ str = "您传的是空"; } return new ServerResponse(str); }
案例(get):
import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; import java.net.URI; import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; public class HttpGetDemo { static String URL = "http://XXX.XXX.XXX.XXX:XXXX/getData.json"; public static void main(String[] args) { //get请求案例。 getRequest(); } public static void getRequest(){ RestTemplate restTemplate = HttpGetDemo.getInstance("utf-8"); StringBuilder result = new StringBuilder(); result.append(restTemplate.getForEntity(URL, String.class).getBody()); System.out.println(result); System.out.println("--------------------------------------------------------------"); //方式二 result = new StringBuilder(); result.append(restTemplate.getForEntity(URL+"?flag={1}", String.class, "方法2").getBody()); System.out.println(result); System.out.println("--------------------------------------------------------------"); //方式三 result = new StringBuilder(); Map<String, String> params = new HashMap<String, String>(); params.put("flag", "方法3"); result.append(restTemplate.getForEntity(URL+"?flag={flag}", String.class, params).getBody()); System.out.println(result); System.out.println("--------------------------------------------------------------"); //方式四 result = new StringBuilder(); UriComponents uriComponents = UriComponentsBuilder.fromUriString( URL+"?flag={flag}") .build() .expand("方法4") .encode(); URI uri = uriComponents.toUri(); result.append(restTemplate.getForEntity(uri, String.class).getBody()); System.out.println(result); } public static RestTemplate getInstance(String charset) { RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(1,new StringHttpMessageConverter(Charset.forName(charset))); return restTemplate; } }
运行结果:
用于测试的Action(controller)。
@RequestMapping(value = "getData.json") public @ResponseBody ServerResponse getData(HttpSession session,@RequestBody People people){ return new ServerResponse(people); }
案例(post):
public class ServerResponse { String msg; String code; Boolean success; People data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public Boolean getSuccess() { return success; } public void setSuccess(Boolean success) { this.success = success; } public People getData() { return data; } public void setData(People data) { this.data = data; } public String toString(){ return "姓名:"+data.getName()+",年龄:"+data.getAge()+"。"; } }
public class People { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
import org.springframework.http.ResponseEntity; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.client.RestTemplate; import java.nio.charset.Charset; public class HttpPostDemo { static String URL = "http://XXX.XXX.XXX.XXX:XXXX/getData.json"; public static void main(String[] args) { //post请求案例。 postRequest(); } public static void postRequest(){ RestTemplate restTemplate = HttpGetDemo.getInstance("utf-8"); People people = new People(); people.setAge("18"); people.setName("李磊"); ResponseEntity<ServerResponse> serverResponseEntity = restTemplate.postForEntity(URL, people,ServerResponse.class); System.out.println(serverResponseEntity.getBody()); System.out.println("--------------------------------------------------------------"); //方式二 people = new People(); people.setAge("20"); people.setName("韩梅梅"); ServerResponse serverResponse = restTemplate.postForObject(URL, people, ServerResponse.class); System.out.println(serverResponse); } public static RestTemplate getInstance(String charset) { RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(1,new StringHttpMessageConverter(Charset.forName(charset))); return restTemplate; } }
运行结果: