•HTTP调用,是一种非常常见的调用技术,业界主流的技术如:Apache HttpClient、JDK自带的.net包下的HttpURLConnection类。
•RestTemplate,它是Spring自带的非常好用的HTTP调用辅助类!
•创建一个IndexController,分别实现一些简单的API接口,然后使用RestTemplate去进行Http调用!
package com.bfxy.springboot.api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.bfxy.springboot.entity.User;
@RestController
public class IndexController {
private static Logger LOGGER = LoggerFactory.getLogger(IndexController.class);
@RequestMapping(value = "/get", produces={"application/json;charset=UTF-8"}, method={RequestMethod.GET})
public User getJson(User user){
System.err.println("---------===== get ====---------");
LOGGER.info("id : " + user.getId() + ", name : " + user.getName());
return user;
}
@RequestMapping(value = "/post", produces={"application/json;charset=UTF-8"}, consumes = {"application/json;charset=UTF-8"}, method={RequestMethod.POST})
public User postJson(@RequestBody User user){
System.err.println("---------===== post ====---------");
LOGGER.info("id : " + user.getId() + ", name : " + user.getName());
return user;
}
@RequestMapping(value = "/exchange", produces={"application/json;charset=UTF-8"}, consumes = {"application/json;charset=UTF-8"}, method={RequestMethod.POST})
public void exchange(@RequestHeader("token")String token, @RequestBody User user){
System.err.println("exchange 4 post token: " + token + ", user: " + user.getName());
}
}
package com.bfxy.springboot.util;
import java.net.URI;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class RestTemplateTest {
private static RestTemplate restTemplate = new RestTemplate();
public static void main(String[] args) throws Exception {
/**
//get:
Map json1 = restTemplate.getForObject("http://localhost:8001/index/get?id=3&name=\"张三\"", Map.class);
System.err.println("get data: " + json1);
//post:
Map<String, String> params = new HashMap<String, String>();
params.put("id", "1003");
params.put("name", "张13");
Map<String, String> json2 = restTemplate.postForObject("http://localhost:8001/index/post", params, Map.class);
System.out.println("post data: " + json2);
*/
//exchange
String body = "{\"id\":\"001\", \"name\": \"张三\"}";
HttpHeaders headers = new HttpHeaders();
headers.set("token", "bfxy");
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> entity = new HttpEntity<String>(body, headers);
ResponseEntity<Map> response = restTemplate.exchange(URI.create("http://localhost:8001/index/exchange"),
HttpMethod.POST,
entity,
Map.class);
System.err.println("response code: " + response.getStatusCode());
}
}