有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我目前正在从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()调用,但是,我不知道如何使用该类生成字符串响应输出


共 (2) 个答案

  1. # 1 楼答案

    通过执行以下操作解决此问题:

            public String toJsonString(Response response)throws IOException 
            {
              JacksonSerializer jacksonSerializer = new JacksonSerializer();
              constructedResponse = jacksonSerializer.serialize(response);
    
              JSONObject jsonObject = new JSONObject();
    
              jsonObject.put("response",constructedResponse);
            }
    
  2. # 2 楼答案

    在我的课堂上,我能够通过以下方式获得字符串:

     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);
     }