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

时间:2018-03-15 16:48:18

标签: java swagger spring-restcontroller jackson-databind

我正在构建一个使用第三方SDK的POC项目。 SDK有一个响应对象,为了进行探索性测试,我需要在控制器中返回相同的响应。直接返回响应会引发异常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]

1 个答案:

答案 0 :(得分:1)

使用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 );