Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot WebTestClient-如何在GraphQLAPI中使用expectBody_Java_Json_Spring Boot_Graphql - Fatal编程技术网

Java SpringBoot WebTestClient-如何在GraphQLAPI中使用expectBody

Java SpringBoot WebTestClient-如何在GraphQLAPI中使用expectBody,java,json,spring-boot,graphql,Java,Json,Spring Boot,Graphql,使用REST API和Spring Boot WebTestClient,我可以很容易地从返回的JSON返回解析的对象,如下所示: Person person = webTestClient .get() .uri("/person/3") .exchange() .expectBody(Person.class)) .returnR

使用REST API和Spring Boot WebTestClient,我可以很容易地从返回的JSON返回解析的对象,如下所示:

  Person person = webTestClient
                .get()
                .uri("/person/3")
                .exchange()
                .expectBody(Person.class))
                .returnResult()
                .getResponseBody();
{ 
  "data" : {
    "person" : {
      "name" : "Foo"
    }
}
使用graphql,json被包装在数据属性中,如下所示:

  Person person = webTestClient
                .get()
                .uri("/person/3")
                .exchange()
                .expectBody(Person.class))
                .returnResult()
                .getResponseBody();
{ 
  "data" : {
    "person" : {
      "name" : "Foo"
    }
}
所以它不适用于

      ...
      .expectBody(Person.class))
因为JSON解析器以“data”而不是“data.person”开头

如何实现直接解析JSON结果并返回Person对象?

GraphQL具有默认结构,任何成功的响应都会在下面的
数据块中返回

{
  "data": { ... },
  "errors": [ ... ]
 }
因此,请使用
@JsonRootName(value=“person”)
并使用
展开根值
功能配置
对象映射器

@JsonRootName(value = "person")
public class Person {

  // properties

  }

 ObjectMapper om = new ObjectMapper();
 om.enable(SerializationFeature.WRAP_ROOT_VALUE);
 om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
GraphQL具有默认结构,任何成功的响应都会在下面的
data
块中返回

{
  "data": { ... },
  "errors": [ ... ]
 }
因此,请使用
@JsonRootName(value=“person”)
并使用
展开根值
功能配置
对象映射器

@JsonRootName(value = "person")
public class Person {

  // properties

  }

 ObjectMapper om = new ObjectMapper();
 om.enable(SerializationFeature.WRAP_ROOT_VALUE);
 om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)

但是如何将其与webtestclient连接?请参见此仅将
ObjectMapper
设置为
webtestclient
@Janning但如何将其与webtestclient连接?请参见此仅将
ObjectMapper
设置为
webtestclient
@Janning