Saramanda9988 opened a new pull request, #4005:
URL: https://github.com/apache/hertzbeat/pull/4005

   ## What's changed?
   
   <!-- Describe Your PR Here -->
   
   The API response format example in the HTTP Service Discovery document does 
not match the actual code implementation, resulting in deserialization errors 
when users implement the API according to the document.
   
   All error examples in the following 4 document files have been fixed
   
   Incorrect format in the document:
   ```json
   {
     "targets": [
       "192.168.1.101:8080",
       "192.168.1.102:8080"
     ]
   }
   ```
   
   log:
   ```text
   2026-01-26 15:57:13 [1000000000-http_sd-target-3115] ERROR 
org.apache.hertzbeat.common.util.JsonUtil - Cannot deserialize value of type 
java.util.ArrayList<org.apache.hertzbeat.common.entity.sd.ServiceDiscoveryResponseEntity>
 from Object value (token JsonToken.START_OBJECT)
   at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION 
disabled); line: 1, column: 1]
   com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot 
deserialize value of type 
java.util.ArrayList<org.apache.hertzbeat.common.entity.sd.ServiceDiscoveryResponseEntity>
 from Object value (token JsonToken.START_OBJECT)
   at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION 
disabled); line: 1, column: 1]
   at 
com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
   at 
com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1767)
   at 
com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1541)
   at 
com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1488)
   at 
com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:402)
   at 
com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:254)
   at 
com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
   at 
com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
   at 
com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4917)
   at 
com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3860)
   at 
com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3843)
   at org.apache.hertzbeat.common.util.JsonUtil.fromJson(JsonUtil.java:82)
   at 
org.apache.hertzbeat.collector.collect.sd.HttpSdCollectImpl.collect(HttpSdCollectImpl.java:87)
   at 
org.apache.hertzbeat.collector.dispatch.MetricsCollect.run(MetricsCollect.java:202)
   at 
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
   at 
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
   at java.base/java.lang.Thread.run(Thread.java:840)
   ```
   
   Code implementation in HttpSdCollectImpl.java:
   ```java
   String responseBody = EntityUtils.toString(response.getEntity(), 
StandardCharsets.UTF_8);
   TypeReference<List<ServiceDiscoveryResponseEntity>> typeReference = new 
TypeReference<>() {};
   final List<ServiceDiscoveryResponseEntity> responseEntityList = 
JsonUtil.fromJson(responseBody, typeReference);
   if (CollectionUtils.isEmpty(responseEntityList)) {
       return;
   }
   ```
   
   Entity definition in ServiceDiscoveryResponseEntity.java:
   ```java
   /**
    * Service Discovery Response Entity
    */
   @Data
   @Builder
   @AllArgsConstructor
   @NoArgsConstructor
   public class ServiceDiscoveryResponseEntity {
       private List<String> target;
   }
   ```
   
   The expected format of the actual code:
   ```java
   [
     {
       "target": [
         "192.168.1.101:8080",
         "192.168.1.102:8080"
       ]
     }
   ]
   ```
   
   ## Checklist
   
   - [x]  I have read the [Contributing 
Guide](https://hertzbeat.apache.org/docs/community/code_style_and_quality_guide)
   - [x]  I have written the necessary doc or comment.
   - [ ]  I have added the necessary unit tests and all cases have passed.
   
   ## Add or update API
   
   - [ ] I have added the necessary [e2e 
tests](https://github.com/apache/hertzbeat/tree/master/e2e) and all cases have 
passed.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to