将 RestTemplate 交换转换为 okHttpClient 调用
Converting RestTemplate exchange to okHttpClient call
我正在尝试用 OkHttpClient 替换 spring 启动应用程序中的 ResteTemplate。
这是我使用来自 Spring 的 RestTemplate 的代码:
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/x-www-form-urlencoded");
HttpEntity<?> httpEntity = new HttpEntity<>("grant_type=client_credentials&scope=" + config.getScope(), headers);
ResponseEntity<Token> resp = getRestTemplate(builder).exchange(
new URI(config.getTokenUrl()),
HttpMethod.POST,
httpEntity,
Token.class);
这是我尝试将该代码映射到 OkHttpClient 的尝试:
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("body", "grant_type=client_credentials&scope=" + config.getScope())
.build();
Request request = new Request.Builder()
.header("Accept", "application/json")
.header("Content-Type", "application/x-www-form-urlencoded")
.url("http://" + config.getTokenUrl())
.post(requestBody)
.build();
OkHttpClient client = buildOkHttpClient();
Response response = client.newCall(request).execute();
Objects.requireNonNull(response.body()).close();
问题是我得到一个错误 Response{protocol=http/1.1, code=405, message=Method Not Allowed
。
如 RestTemplate 中所示,Http 方法是 POST。
但我不确定我应该如何 map/transform HttpEntity<?> httpEntity = new HttpEntity<>("grant_type=client_credentials&scope=" + config.getScope(), headers);
来符合 OkHttp?
也许错误就在这里?
感谢任何帮助!
检查 this link 和这个结构:
RequestBody requestBody = new FormBody.Builder()
.add("grant_type", "client_credentials")
.add("scope", config.getScope())
.build();
Request request = new Request.Builder()
.url("http://" + config.getTokenUrl())
.post(requestBody)
.build();
我正在尝试用 OkHttpClient 替换 spring 启动应用程序中的 ResteTemplate。
这是我使用来自 Spring 的 RestTemplate 的代码:
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/x-www-form-urlencoded");
HttpEntity<?> httpEntity = new HttpEntity<>("grant_type=client_credentials&scope=" + config.getScope(), headers);
ResponseEntity<Token> resp = getRestTemplate(builder).exchange(
new URI(config.getTokenUrl()),
HttpMethod.POST,
httpEntity,
Token.class);
这是我尝试将该代码映射到 OkHttpClient 的尝试:
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("body", "grant_type=client_credentials&scope=" + config.getScope())
.build();
Request request = new Request.Builder()
.header("Accept", "application/json")
.header("Content-Type", "application/x-www-form-urlencoded")
.url("http://" + config.getTokenUrl())
.post(requestBody)
.build();
OkHttpClient client = buildOkHttpClient();
Response response = client.newCall(request).execute();
Objects.requireNonNull(response.body()).close();
问题是我得到一个错误 Response{protocol=http/1.1, code=405, message=Method Not Allowed
。
如 RestTemplate 中所示,Http 方法是 POST。
但我不确定我应该如何 map/transform HttpEntity<?> httpEntity = new HttpEntity<>("grant_type=client_credentials&scope=" + config.getScope(), headers);
来符合 OkHttp?
也许错误就在这里?
感谢任何帮助!
检查 this link 和这个结构:
RequestBody requestBody = new FormBody.Builder()
.add("grant_type", "client_credentials")
.add("scope", config.getScope())
.build();
Request request = new Request.Builder()
.url("http://" + config.getTokenUrl())
.post(requestBody)
.build();