ResponseBuilder toString()以字符串形式返回对象类,而不仅仅是原始响应字符串

2020年8月5日 33点热度 0条评论

我目前正在从Java ASK-SDK v1迁移到Java ASK SDK v2

我正在尝试使用构建响应的ResponseBuilder类返回webhook调用,并且数据正确,但是当我尝试用JSON文本填充HTTP正文时,ResponseBuilder.toString()值不只是仅使用字符串填充数据,我得到以下信息:

Optional[class Response {
    outputSpeech: class SsmlOutputSpeech {
        class OutputSpeech {
            type: SSML
            playBehavior: null
        }
        ssml: <speak>Some of the things you can say are What would you like to do?</speak>
    }
    card: null
    reprompt: class Reprompt {
        outputSpeech: class SsmlOutputSpeech {
            class OutputSpeech {
                type: SSML
                playBehavior: null
            }
            ssml: <speak>You can say ..., is that what you want?</speak>
        }
    }
    directives: []
    shouldEndSession: false
    canFulfillIntent: null
}]

是否有另一种方法来获取响应主体的字符串?
BaseSkillResponse有一个
getResponse()调用,但是,我无法弄清楚如何使用该类生成String响应输出。

解决方案如下:

我可以在课堂上取得以下内容的字串:

 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

 myFunction(){
     try{
         return toJsonString(responseBuilder.build().get());
     } catch(IOException e) {
         e.printStackTrace();
     }
 }

 public String toJsonString(Response response)throws IOException {
     return OBJECT_MAPPER.writeValueAsString(response);
 }