[英]How to check response from the RestTemplate for error and success case for the JSON String?
[英]How to check the response from an API through restTemplate?
我知道回應是什么。 但我很好奇,想在反序列化之前了解響應。
下面是向某個 API 發送 HTTP 請求。
val response = restTemplate.exchange(
url,
HttpMethod.POST,
requestEntity,
DesignatedDto::class.java
)
我曾經遇到過上述問題,導致 API 文檔有點錯誤,因此DesignatedDto
無法反序列化響應。 現在我在檢查了發送給 API 程序員的真實響應后解決了這個問題。
但是現在我想知道作為一名程序員是否有一種方法可以在反序列化之前了解響應而無需通過電子郵件發送? 請不要說我應該使用String::class.java
而不是DesignatedDto::class.java
因為我有如下錯誤
org.springframework.web.client.RestClientException: Error while extracting response for type [class java.lang.String] and content type [text/html;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 6, column: 1]
您甚至不需要 ResponseEntity。 只需將 getForObject 與 String.class 一起使用
String response = restTemplate.postForObject(URL,request,String.class);
但是當服務器發送application/json作為內容類型時,這將不起作用。 如果正文只包含 json 字符串,它只會給你一個字符串
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.