Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 如何从gcp中的spring云函数返回自定义http响应代码?_Kotlin_Google Cloud Platform_Spring Cloud Function - Fatal编程技术网

Kotlin 如何从gcp中的spring云函数返回自定义http响应代码?

Kotlin 如何从gcp中的spring云函数返回自定义http响应代码?,kotlin,google-cloud-platform,spring-cloud-function,Kotlin,Google Cloud Platform,Spring Cloud Function,我们正在使用Java/Kotlin使用新的gcp云函数 与当前的参考实现一样,我们返回org.springframework.messaging.support.GenericMessage对象 因此,我们的代码如下所示(Kotlin): fun generatePdfInBase64(message:message):message{ val文档=进程(消息) val encoded=Base64.getEncoder().encodeToString(document.document) 返

我们正在使用Java/Kotlin使用新的gcp云函数

与当前的参考实现一样,我们返回org.springframework.messaging.support.GenericMessage对象

因此,我们的代码如下所示(Kotlin):

fun generatePdfInBase64(message:message):message{
val文档=进程(消息)
val encoded=Base64.getEncoder().encodeToString(document.document)
返回GenericMessage(已编码)
}
我们无法找到任何方法将自定义http响应代码包含到消息中,例如201或其他内容。如果没有异常,该功能仅响应200或500

有人知道这样做的方法吗

祝福


Andy

正如官方所提到的,HttpResponse类有一个名为的方法,您可以在该方法中设置状态的编号作为方便

例如:

 switch (request.getMethod()) {
      case "GET":
        response.setStatusCode(HttpURLConnection.HTTP_OK);
        writer.write("Hello world!");
        break;
    
另一方面,GenericMessage的参数接收一个有效负载,因此我认为您可以创建一个json格式的字符串,并使用构造函数创建您需要的状态响应的GenericMessage实例


如果您想了解更多关于statuds代码的信息,请查看此信息。

谢谢您的回答。但是,我们没有找到任何方法将状态代码插入到GenericMessage的constructur/headers中。
 switch (request.getMethod()) {
      case "GET":
        response.setStatusCode(HttpURLConnection.HTTP_OK);
        writer.write("Hello world!");
        break;