Java webclient在字符串响应上返回2个双引号

问题描述

@RestController
@RequestMapping("/foo")
public class FooController {

  @GetMapping
  @ApiImplicitParams({
    @ApiImplicitParam(
        name = "page",dataType = "int",paramType = "query",value = "Page you want to retrieve",defaultValue = "0"),@ApiImplicitParam(
        name = "size",value = "Number of foo per page.",defaultValue = "10"))
  public Slice<Foo> getFoo(Pageable pageable) {
        return service.findAllInactive(pageable);
    }
}

我使用上述方法如下。

enter image description here

如您所见,响应显示字符串值的 2 个双引号。因此,我无法将其解析为 ObjectID。

我做错了什么?

为什么它返回 2 个双引号值 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()); } } 而不是 1 个双引号值 ""60cc845e86114119f8aa4306""

如何防止 2 个双引号值并检索 1 个双引号值?

解决方法

看起来当我返回 JSON 响应并使用 bodyToMono(String.class) 时,它会将其字符串化。

当我从服务器返回字符串时,bodyToMono(String.class) 也会对其进行字符串化并添加额外的引号。目前,我已经使用子字符串手动删除了它。