一、請求代碼以下java
(1)使用OkHttp客戶端發送http請求web
http://localhost:8089/test/sendOkHttp?timeout=5000spring
(2)使用HttpClient客戶端發送http請求json
http://localhost:8089/test/sendkHttpClient?timeout=5000 app
import com.iqianjin.commons.utils.JsonUtil; import com.iqianjin.user.service.base.CommonResult; import com.iqianjin.user.service.config.MoonUser; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; @Slf4j @RestController @RequestMapping("/test") public class TestTemplateController { @Qualifier("okHttpRestTemplate") @Autowired private RestTemplate okHttpRestTemplate; @Qualifier("httpClientRestTemplate") @Autowired private RestTemplate httpClientRestTemplate; private String testUrl = "http://localhost:8089/test/receive"; @PostMapping("/receive") public CommonResult receive(@RequestBody MoonUser moonUser) { try { Thread.sleep(moonUser.getTimeout()); return CommonResult.successData(moonUser); } catch (Exception e) { log.error("receive出現異常", e); return CommonResult.failMsg(e.getMessage()); } } @GetMapping("/sendOkHttp") public Object sendOkHttp(Long timeout) { try { timeout = timeout == null ? 0 : timeout; MoonUser moonUser = new MoonUser(); moonUser.setId(16L); moonUser.setName("冷月"); moonUser.setTimeout(timeout); return postForJsonContentType(okHttpRestTemplate, testUrl, moonUser); } catch (Exception e) { log.error("出現異常", e); return "發生了異常"; } } @GetMapping("/sendHttpClient") public Object sendHttpClient(Long timeout) { try { timeout = timeout == null ? 0 : timeout; MoonUser moonUser = new MoonUser(); moonUser.setId(16L); moonUser.setName("冷月"); moonUser.setTimeout(timeout); return postForJsonContentType(httpClientRestTemplate, testUrl, moonUser); } catch (Exception e) { log.error("出現異常", e); return "發生了異常"; } } public String postForJsonContentType(RestTemplate restTemplate, String url, Object reqObject) { String body = JsonUtil.toJsonString(reqObject); log.info("【restTemplate】post 請求url={},請求={}", url, body); HttpHeaders headers = new HttpHeaders(); // 定義請求參數類型,這裏用json因此是MediaType.APPLICATION_JSON headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<?> request = new HttpEntity<>(reqObject, headers); ResponseEntity<String> entity = restTemplate.postForEntity(url, request, String.class); String resp = entity.getBody(); log.info("【restTemplate】post 請求url={},響應={}", url, resp); return resp; }
public class MoonUser { private Long id; private String name; private Long timeout; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getTimeout() { return timeout; } public void setTimeout(Long timeout) { this.timeout = timeout; } }