diff options
| author | shizhendong <[email protected]> | 2021-11-02 18:13:07 +0300 |
|---|---|---|
| committer | shizhendong <[email protected]> | 2021-11-02 18:13:07 +0300 |
| commit | 56ba8a11f89b1b6c2c578007284730c7923b6b1e (patch) | |
| tree | 8e29b3f5555a9cbc1eff89927155b28452b5bff2 | |
| parent | eaa63672bf7b873c47932b64628b7d784bdfc2df (diff) | |
fix: 修改 restTemplate 对 url 再次编码问题
| -rw-r--r-- | src/main/java/net/geedge/confagent/controller/CortexController.java | 54 |
1 files changed, 26 insertions, 28 deletions
diff --git a/src/main/java/net/geedge/confagent/controller/CortexController.java b/src/main/java/net/geedge/confagent/controller/CortexController.java index 5c255a3..e78fdab 100644 --- a/src/main/java/net/geedge/confagent/controller/CortexController.java +++ b/src/main/java/net/geedge/confagent/controller/CortexController.java @@ -1,30 +1,8 @@ package net.geedge.confagent.controller; -import java.io.OutputStream; -import java.util.Arrays; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -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.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; - +import cn.hutool.core.net.url.UrlBuilder; +import cn.hutool.core.net.url.UrlPath; +import cn.hutool.core.net.url.UrlQuery; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.log.Log; @@ -35,6 +13,22 @@ import net.geedge.confagent.util.ConfagentUtil; import net.geedge.confagent.util.R; import net.geedge.confagent.util.RCode; import net.geedge.confagent.util.Tool; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +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.client.RestTemplate; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.OutputStream; +import java.net.URI; +import java.nio.charset.Charset; +import java.util.*; +import java.util.Map.Entry; @RestController @RequestMapping("/cortex") @@ -189,10 +183,14 @@ public class CortexController extends BaseController { HttpEntity<byte[]> httpEntity = new HttpEntity<byte[]>(body, headers); String queryString = request.getQueryString(); queryString = StrUtil.isNotBlank(queryString) ? queryString : ""; - String url = String.format("http://%s:%s%s?%s", defaultCortexIP,port,path,Tool.StrUtil.blankToDefault(queryString, "")); - log.debug("cortex url: {}", url); + // String url = String.format("http://%s:%s%s?%s", defaultCortexIP,port,path,Tool.StrUtil.blankToDefault(queryString, "")); + String urlStr = UrlBuilder.create().setScheme("http").setHost(defaultCortexIP) + .setPort(port).setPath(UrlPath.of(path, Charset.forName("UTF-8"))) + .setQuery(UrlQuery.of(Tool.StrUtil.blankToDefault(queryString, ""), Charset.forName("UTF-8"))).toString(); + + log.info("cortex url: {}", urlStr); String method = request.getMethod(); - ResponseEntity<byte[]> exchange = restTemplate.exchange(url, HttpMethod.resolve(method), httpEntity, + ResponseEntity<byte[]> exchange = restTemplate.exchange(new URI(urlStr), HttpMethod.resolve(method), httpEntity, byte[].class); int statusCode = exchange.getStatusCodeValue(); HttpHeaders httpHeaders = exchange.getHeaders(); |
