gpt4 book ai didi

java - 返回第三方响应对象 - com.fasterxml.jackson.databind.JsonMappingException

转载 作者:行者123 更新时间:2023-11-30 06:15:26 25 4
gpt4 key购买 nike

我正在构建一个使用第三方 SDK 的 POC 项目。 SDK 有一个响应对象,为了进行探索性测试,我需要在 Controller 中返回相同的响应。返回响应直接抛出异常 com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class

所以我将此响应包装为字符串:

    ThirdPartyResponseObject res = myService.getSomething();
ObjectMapper mapper = new ObjectMapper();
mapper.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
String someJsonString = null;
try {
someJsonString = mapper.writeValueAsString(res);
} catch ( JsonProcessingException e ) {

}
return new ResponseEntity<>( someJsonString, HttpStatus.OK );

这会以字符串形式返回响应(例如 {"context":{"errors":[],"errorCount":0,"level":0...),即很好,但在 Swagger 中查看响应并不友好,因为字符串太长并且无法看到 json 元素的层次结构。

所以我尝试将此 json 字符串解析为 JsonElement 并通过以下方式返回 json 元素:

ThirdPartyResponseObject res = myService.getSomething();
ObjectMapper mapper = new ObjectMapper();
mapper.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
String someJsonString = null;
JsonElement jsonElement = null;
try {
someJsonString = mapper.writeValueAsString(res);
JsonParser parser = new JsonParser();
jsonElement = parser.parse( someJsonString );
} catch ( JsonProcessingException e ) {

}
return new ResponseEntity<>( jsonElement, HttpStatus.OK );

但这会引发异常com.fasterxml.jackson.databind.JsonMappingException:JsonObject

我需要返回响应,使其格式为 json。像这样的东西:

{
"context":
{
"errors":[],
"errorCount":0,
"level":0
...
}
}

已更新以添加以下错误详细信息:

Could not write JSON: JsonObject; nested exception is com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asString"])
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: JsonObject; nested exception is com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asString"])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:106) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:231) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:203) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]

最佳答案

使用 JsonNode 对我有用。代码如下所示:

    ThirdParyResponseObject res = myService.getSomething();
ObjectMapper mapper = new ObjectMapper();
mapper.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
String someJsonString = null;
JsonNode node = null;
try {
someJsonString = mapper.writeValueAsString(res);
node = mapper.readTree(someJsonString);
return new ResponseEntity<>( node, HttpStatus.OK );
} catch ( JsonProcessingException e ) {
//log.error( e );
} catch ( IOException e ) {
//log.error( e );
}
return new ResponseEntity<>( node, HttpStatus.OK );

关于java - 返回第三方响应对象 - com.fasterxml.jackson.databind.JsonMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49304977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com