简体   繁体   English

从 SpringBootApplication 中的 ClientResponse 获取正文

[英]Get body from ClientResponse in SpringBootApplication

I'm new to spring boot application.我是 spring 引导应用程序的新手。 I want to get the response from there in string.我想从那里得到字符串的响应。 this is my code.这是我的代码。

String accessURL = serverURL + "itim/rest/accesscategories";
        RestClient rc4 = new RestClient(ResourceBuilder.getClientConfig());
        Resource r4 = rc4.resource(accessURL);
        r4.cookie(ltpaToken);
        r4.cookie(jsessionid);
        r4.cookie(csrftoken);
        r4.header("Content-Type", "application/json");
        ClientResponse resp4 = r4.get();

right now i'm doing this:现在我正在这样做:

System.out.println(resp4.getStatusCode());
        System.out.println(resp4.getStatusType());
        System.out.println(resp4.getHeaders());

and i get this responce:我得到了这个回应:

200
OK
CaseInsensitiveMultivaluedMap [map=[Cache-Control=no-cache,no-store,max-age=0,Content- 
Language=en-US,Content-Type=application/vnd.ibm.isim-v1+json,Date=Sat, 24 Apr 2021 02:22:32 
GMT,Expires=Thu, 01 Jan 1970 00:00:00 GMT,Pragma=No-cache,Set- 
Cookie=com.ibm.isim.lastActivity=Bj7WAcMo3apevFKD4JoHyr4iKepdA8AVptb3S7eRM5c%3D; Path=/itim; 
Secure; HttpOnly,Set-Cookie=com.ibm.isim.maxInactive=1800; Path=/itim; Secure,Strict- 
Transport-Security=max-age=31536000; includeSubdomains,Strict-Transport-Security=max- 
age=31536000; includeSubdomains,Transfer-Encoding=chunked,X-FRAME-OPTIONS=SAMEORIGIN]]

Use response.getBody() function or simply response.body if you're using any frontend framework.如果您使用任何前端框架,请使用 response.getBody() function 或简单的 response.body。

You probably want to do something like this, so you can process the JSON response (unless you already have JSON serialization/de-serialization for a POJO set up elsewhere).你可能想做这样的事情,所以你可以处理 JSON 响应(除非你已经有 JSON 序列化/反序列化,用于在别处设置的 POJO)。

JsonNode body = resp4.bodyToMono( JsonNode.class ).block();

You could do something simple like this to test.你可以做一些像这样简单的测试。

System.out.println( resp4.bodyToMono( String.class ).block() );

Take a look at the documentation for ClientResponse and Mono<T> for more details.查看ClientResponseMono<T>的文档以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM