接口代理
This commit is contained in:
parent
b14d02e6ef
commit
09001f5610
|
@ -1,31 +1,59 @@
|
||||||
package com.nu.modules.proxy;
|
package com.nu.modules.proxy;
|
||||||
|
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/proxy")
|
@RequestMapping("/api/proxy")
|
||||||
public class ProxyApi {
|
public class ProxyApi {
|
||||||
|
|
||||||
@Autowired
|
private final RestTemplate restTemplate = new RestTemplate(); // 不使用配置类,直接实例化
|
||||||
private RestTemplate restTemplate;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理 GET 请求
|
||||||
|
* @param apiUrl 三方接口的完整 URL
|
||||||
|
* @param params 请求的查询参数
|
||||||
|
* @return 代理后的响应
|
||||||
|
*/
|
||||||
@GetMapping("/get")
|
@GetMapping("/get")
|
||||||
public ResponseEntity<String> proxyGet(@RequestParam String url,
|
public ResponseEntity<String> proxyGet(
|
||||||
@RequestHeader(required = false) HttpHeaders headers) {
|
@RequestParam String apiUrl,
|
||||||
|
@RequestParam(required = false) String params) {
|
||||||
|
|
||||||
|
String fullUrl = apiUrl + (params != null ? "?" + params : "");
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
||||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
|
|
||||||
return ResponseEntity.ok(response.getBody());
|
// 发起 GET 请求
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(fullUrl, HttpMethod.GET, entity, String.class);
|
||||||
|
|
||||||
|
return response; // 返回代理后的响应
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理 POST 请求
|
||||||
|
* @param apiUrl 三方接口的完整 URL
|
||||||
|
* @param body 请求体
|
||||||
|
* @return 代理后的响应
|
||||||
|
*/
|
||||||
@PostMapping("/post")
|
@PostMapping("/post")
|
||||||
public ResponseEntity<String> proxyPost(@RequestParam String url,
|
public ResponseEntity<String> proxyPost(
|
||||||
@RequestBody(required = false) String body,
|
@RequestParam String apiUrl,
|
||||||
@RequestHeader(required = false) HttpHeaders headers) {
|
@RequestBody String body) {
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
||||||
HttpEntity<String> entity = new HttpEntity<>(body, headers);
|
HttpEntity<String> entity = new HttpEntity<>(body, headers);
|
||||||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
|
|
||||||
return ResponseEntity.ok(response.getBody());
|
// 发起 POST 请求
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);
|
||||||
|
|
||||||
|
return response; // 返回代理后的响应
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue