Java webclient returns 字符串响应上有 2 个双引号
Java webclient returns 2 double quotes on string response
package com.myproject.extract.Generic;
import com.myproject.extract.Callbacks.catchError;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import org.bson.types.ObjectId;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
public class ajax {
public static Mono<String> Post(String url, ObjectId projectId, JsonObject data) {
try {
// To call https, following is required.
SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));
WebClient webClient = WebClient.builder().baseUrl("https://localhost:3100/api/" + url)
.defaultHeader("projectId", projectId.toString()) // TODO: there was option of only header instead
// of
// defaultheader. don't know why that was not
// working. don't know the difference either.
.clientConnector(new ReactorClientHttpConnector(httpClient)).build();
return webClient.post().syncBody(toJsonNode(data)).retrieve().bodyToMono(String.class);
} catch (Exception e) {
catchError.handlError(e);
}
return null;
}
public static JsonNode toJsonNode(JsonObject jsonObj) throws Exception {
return new ObjectMapper().readTree(jsonObj.toString());
}
}
我正在使用上面的方法如下。
如您所见,响应为字符串值显示了 2 个双引号。因此,我无法将其解析为 ObjectID。
我做错了什么?
为什么 returns 2 个双引号值 ""60cc845e86114119f8aa4306""
而不是 1 个双引号值 "60cc845e86114119f8aa4306"
?
如何防止 2 个双引号值并检索 1 个双引号值?
看起来当我 return JSON 响应并且我使用 bodyToMono(String.class)
时,它会将其字符串化。
当我 return 来自服务器的字符串时,bodyToMono(String.class)
也会将其字符串化并添加额外的引号。现在,我已经使用子字符串手动删除了它。
package com.myproject.extract.Generic;
import com.myproject.extract.Callbacks.catchError;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import org.bson.types.ObjectId;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
public class ajax {
public static Mono<String> Post(String url, ObjectId projectId, JsonObject data) {
try {
// To call https, following is required.
SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));
WebClient webClient = WebClient.builder().baseUrl("https://localhost:3100/api/" + url)
.defaultHeader("projectId", projectId.toString()) // TODO: there was option of only header instead
// of
// defaultheader. don't know why that was not
// working. don't know the difference either.
.clientConnector(new ReactorClientHttpConnector(httpClient)).build();
return webClient.post().syncBody(toJsonNode(data)).retrieve().bodyToMono(String.class);
} catch (Exception e) {
catchError.handlError(e);
}
return null;
}
public static JsonNode toJsonNode(JsonObject jsonObj) throws Exception {
return new ObjectMapper().readTree(jsonObj.toString());
}
}
我正在使用上面的方法如下。
如您所见,响应为字符串值显示了 2 个双引号。因此,我无法将其解析为 ObjectID。
我做错了什么?
为什么 returns 2 个双引号值 ""60cc845e86114119f8aa4306""
而不是 1 个双引号值 "60cc845e86114119f8aa4306"
?
如何防止 2 个双引号值并检索 1 个双引号值?
看起来当我 return JSON 响应并且我使用 bodyToMono(String.class)
时,它会将其字符串化。
当我 return 来自服务器的字符串时,bodyToMono(String.class)
也会将其字符串化并添加额外的引号。现在,我已经使用子字符串手动删除了它。