@Configuration
public class RestConfiguration {
@Autowired
private RestTemplateBuilder builder;
@Bean
public RestTemplate restTemplate(){
builder.setConnectTimeout(10000);
builder.setReadTimeout(100000);
return builder.build();
}
}
public class HttpRequestUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpRequestUtil.class);
public static String sendWithSpringRest(String url,String json,HttpMethod httpMethod) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application","json",Charset.forName("UTF-8")));
headers.setAccept(Arrays.asList(new MediaType[] {new MediaType("application","json",Charset.forName("UTF-8"))}));
HttpEntity<String> requestEntity = new HttpEntity<String>(json,headers);
ResponseEntity<String> exchange = restTemplate.exchange(url, httpMethod,requestEntity,String.class);
String body = exchange.getBody();
logger.info("-------------->"+body);
return body;
}
}
@Autowired
private RestTemplate restTemplate;
@GetMapping("/forObject")
public Map<String, Object> testForObject(Map<String, Object> params){
Map<String, Object> retMap = new HashMap<String, Object>();
Map<String, Object> paramsRest = new HashMap<String, Object>();
paramsRest.put("userName", "用户名");
paramsRest.put("userPwd", "123456");
String urlTemplate = "http://localhost:8888/rest"+"?userName={userName}&userPwd={userPwd}";
Map forObject = this.restTemplate.getForObject(urlTemplate,Map.class,paramsRest);
System.out.println("--------result------------->"+forObject);
return retMap;
}
@RequestMapping("/exchange")
public Map<String, Object> testExchange(){
String url = "http://localhost:8888/rest";
HttpHeaders headers = new HttpHeaders();
// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 封装参数,千万不要替换为Map与HashMap,否则参数无法传递
MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
// 也支持中文
params.add("userName", "用户名");
params.add("userPwd", "123456");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
// 执行HTTP请求
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
// 输出结果
System.out.println("-----------responseBody------------>"+response.getBody());
return null;
}
@PostMapping("/hello3")
public String query3(@RequestBody UserInfo userInfo) {
System.out.println("---------------"+JSONObject.toJSONString(userInfo));
HttpHeaders headers = new HttpHeaders();
//JSONObject params = new JSONObject();
//JSONObject.
HttpEntity<UserInfo> requestEntity = new HttpEntity<UserInfo>(userInfo, headers);
String url = "http://localhost:8090/api/hello3";
// 执行HTTP请求
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
long endTime = System.currentTimeMillis();
String body = response.getBody();
//JSONObject body = JSONObject.parseObject(response.getBody());
System.out.println(body);
return body;
}
用exchange主要是在设置一些请求头的时候比较方便,我更多时候会用exchange
@PostMapping("/hello4")
public String query4(String grantType,String userName,String password) {
System.out.println("---------------"+grantType+",userName="+userName+",password"+password);
HttpHeaders headers = new HttpHeaders();
//headers.set("Authorization","Basic YW5kcm9pZDphbmRyb2lk");
/*JSONObject params = new JSONObject();
params.put("grant_type",grantType);
params.put("username",userName);
params.put("password",password);*/
//HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(params, headers);
String url = "http://android:android@localhost:1202/auth/oauth/token?grant_type="+grantType+"&username="+userName+"&password="+password;
// 执行HTTP请求
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, null, String.class);
String body = response.getBody();
System.out.println("==================="+body);
//JSONObject body = JSONObject.parseObject(response.getBody());
System.out.println(body);
return body;
}