SpringBoot使用RestTemplate03模拟请求

编程

1、请求代码如下

(1)使用OkHttp客户端发送http请求

http://localhost:8089/test/sendOkHttp?timeout=5000

(2)使用HttpClient客户端发送http请求

http://localhost:8089/test/sendkHttpClient?timeout=5000 

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;

}

}

 

以上是 SpringBoot使用RestTemplate03模拟请求 的全部内容, 来源链接: utcz.com/z/511144.html

回到顶部