Last active
February 24, 2025 06:37
-
-
Save Hanchers/c0e64954ceefb7e9635a171da6d97e86 to your computer and use it in GitHub Desktop.
spring6.x http interface demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor { | |
| private static final Logger logger = LoggerFactory.getLogger(LoggingRequestInterceptor.class); | |
| @Override | |
| public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { | |
| // 打印请求信息 | |
| logRequest(request, body); | |
| // 执行请求 | |
| ClientHttpResponse response = new RepeatReadClientHttpRequestWrapper(execution.execute(request, body)); | |
| // 打印响应信息 | |
| logResponse(response); | |
| return response; | |
| } | |
| private void logRequest(HttpRequest request, byte[] body) { | |
| logger.debug("Request URI: {} {}",request.getMethod(), request.getURI()); | |
| logger.debug("Request Headers: {}", request.getHeaders()); | |
| if (body.length > 0) { | |
| logger.debug("Request Body: {}", new String(body, StandardCharsets.UTF_8)); | |
| } | |
| } | |
| private void logResponse(ClientHttpResponse response) throws IOException { | |
| logger.debug("Response Code: {} , Headers: {}", response.getStatusCode(),response.getHeaders()); | |
| StringBuilder responseBody = new StringBuilder(); | |
| try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getBody(), StandardCharsets.UTF_8))) { | |
| String line; | |
| while ((line = reader.readLine()) != null) { | |
| responseBody.append(line); | |
| } | |
| } | |
| if (responseBody.length() > 0) { | |
| logger.debug("Response Body: {}", responseBody.toString()); | |
| } | |
| } | |
| /** | |
| * 返回的响应流只能读取一次,所以需要重新封装一个类,实现ClientHttpResponse接口,重写getBody方法,返回一个InputStream, | |
| */ | |
| public class RepeatReadClientHttpRequestWrapper implements ClientHttpResponse { | |
| private ClientHttpResponse response; | |
| private byte[] bodyData = null; | |
| public RepeatReadClientHttpRequestWrapper(ClientHttpResponse response) { | |
| this.response = response; | |
| } | |
| @Override | |
| public HttpStatusCode getStatusCode() throws IOException { | |
| return response.getStatusCode(); | |
| } | |
| @Override | |
| public String getStatusText() throws IOException { | |
| return response.getStatusText(); | |
| } | |
| @Override | |
| public void close() { | |
| response.close(); | |
| } | |
| @Override | |
| public InputStream getBody() throws IOException { | |
| if (Objects.isNull(bodyData)) { | |
| bodyData = response.getBody().readAllBytes(); | |
| } | |
| return new ByteArrayInputStream(bodyData); | |
| } | |
| @Override | |
| public HttpHeaders getHeaders() { | |
| return response.getHeaders(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface TestApi { | |
| @GetExchange(value = "/queryTest",accept = "application/json") | |
| TestRes test(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, | |
| @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, | |
| @RequestParam(value = "jobName",required = false) String jobName | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Data | |
| public class TestBaseRes { | |
| private String msg; | |
| private Integer code; | |
| private Boolean success; | |
| } | |
| @Data | |
| public class TestRes extends TestBaseRes { | |
| private TestData data; | |
| @Data | |
| public static class TestData { | |
| private Integer total; | |
| private JSONArray rows; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Configuration | |
| public class TestConfig { | |
| /** | |
| * 注册TestApi bean | |
| */ | |
| @Bean | |
| public TestApi testApi() { | |
| RestClient restClient = RestClient.builder(). | |
| baseUrl("http://localhost:8080/") | |
| .requestInterceptor(new LoggingRequestInterceptor()) | |
| .build(); | |
| RestClientAdapter adapter = RestClientAdapter.create(restClient); | |
| HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build(); | |
| return factory.createClient(TestApi.class); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment