Google cloud platform 未调用带有CustomAttributes的PubSub-Receiver函数,但出现json解析异常

Google cloud platform 未调用带有CustomAttributes的PubSub-Receiver函数,但出现json解析异常,google-cloud-platform,google-cloud-pubsub,Google Cloud Platform,Google Cloud Pubsub,谷歌云和PubSub服务 我在下面的示例代码行中实现了一个简单的CustomAttributeReceiver 我是如何得到以下例外的 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为字符串,但在com.google.gson.gson.fromJson(gson.java:944)的路径$处为BEGIN_对象 fromJson(gson.java:1003)位于 com.google.cloud.fu

谷歌云和PubSub服务

我在下面的示例代码行中实现了一个简单的CustomAttributeReceiver

我是如何得到以下例外的

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为字符串,但在com.google.gson.gson.fromJson(gson.java:944)的路径$处为BEGIN_对象 fromJson(gson.java:1003)位于 com.google.cloud.functions.invoker.NewBackgroundFunctionExecutor$TypedFunctionExecutor.serviceLegacyEvent(NewBackgroundFunctionExecutor.java:257)位于 com.google.cloud.functions.invoker.NewBackgroundFunctionExecutor.serviceLegacyEvent(NewBackgroundFunctionExecutor.java:343)位于

有人能解释一下我在这里遗漏了什么吗

发行方

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.api.core.ApiFuture;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.TopicName;
import PubSubMessage;


            TopicName topicName = TopicName.of(projectId, topicId);
        System.out.println("informListenersAboutSucessfulRegisteration=" + topicName);
        Publisher publisher = null;
        try {
            publisher = Publisher.newBuilder(topicName).build();
            PubSubMessage newUserRegisterMsg = new PubSubMessage();
            Map<String, String> attributes = new HashMap<String, String>();
            attributes.put(PubSubMessage.newUserLanguage, newUserLanguage);
            newUserRegisterMsg.setAttributes(attributes);
            ByteString data = ByteString.copyFromUtf8("NewUserRegisteration");
            PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).putAllAttributes(attributes).build();
            ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
            String messageId = messageIdFuture.get();
            System.out.println("Published message ID: " + messageId);
        } catch (Exception e) {
            Logger.getLogger(PubSubMessenger.name).log(Level.SEVERE, "Exception while publishing message", e);
        } finally {
            if (publisher != null) {
                publisher.shutdown();
                publisher.awaitTermination(1, TimeUnit.MINUTES);
            }
        }
    import java.nio.charset.StandardCharsets;
    import java.util.Base64;
    
    import com.google.cloud.functions.BackgroundFunction;
    import com.google.cloud.functions.Context;
    import PubSubMessage;

    
public class SendEmailFromSendGrid implements BackgroundFunction<PubSubMessage> {
    public SendEmailFromSendGrid() {
    }

    public void accept(PubSubMessage message, Context context) throws Exception {
        System.out.println("invoked accept");
        String name = "World";
        if (message != null && message.getData() != null) {
            name = new String(Base64.getDecoder().decode(message.getData().getBytes(StandardCharsets.UTF_8)),
                    StandardCharsets.UTF_8);
        }
        System.out.println(String.format("Hello %s!", name));
        return;
    }
}
import java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.concurrent.ExecutionException;
导入java.util.concurrent.TimeUnit;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入com.google.api.core.ApiFuture;
导入com.google.cloud.pubsub.v1.Publisher;
导入com.google.protobuf.ByteString;
导入com.google.pubsub.v1.PubsubMessage;
导入com.google.pubsub.v1.TopicName;
导入公共消息;
TopicName TopicName=TopicName.of(projectId,topicId);
System.out.println(“InformListenerAboutsAccessfulRegistration=“+topicName”);
Publisher=null;
试一试{
publisher=publisher.newBuilder(topicName.build();
PubSubMessage newUserRegisterMsg=新PubSubMessage();
Map attributes=newhashmap();
attributes.put(pubsubessage.newUserLanguage,newUserLanguage);
newUserRegisterMsg.setAttributes(属性);
ByteString数据=ByteString.copyFromUtf8(“新用户注册”);
PubsubMessage PubsubMessage=PubsubMessage.newBuilder().setData(数据).PutalAttribute(属性).build();
ApiFuture messageIdFuture=publisher.publish(pubsubMessage);
字符串messageId=messageIdFuture.get();
System.out.println(“已发布消息ID:+messageId”);
}捕获(例外e){
Logger.getLogger(pubsubsessenger.name).log(Level.SEVERE,“发布消息时异常”,e);
}最后{
if(publisher!=null){
publisher.shutdown();
发布者。等待终止(1,时间单位。分钟);
}
}
接收器侧

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.api.core.ApiFuture;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.TopicName;
import PubSubMessage;


            TopicName topicName = TopicName.of(projectId, topicId);
        System.out.println("informListenersAboutSucessfulRegisteration=" + topicName);
        Publisher publisher = null;
        try {
            publisher = Publisher.newBuilder(topicName).build();
            PubSubMessage newUserRegisterMsg = new PubSubMessage();
            Map<String, String> attributes = new HashMap<String, String>();
            attributes.put(PubSubMessage.newUserLanguage, newUserLanguage);
            newUserRegisterMsg.setAttributes(attributes);
            ByteString data = ByteString.copyFromUtf8("NewUserRegisteration");
            PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).putAllAttributes(attributes).build();
            ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
            String messageId = messageIdFuture.get();
            System.out.println("Published message ID: " + messageId);
        } catch (Exception e) {
            Logger.getLogger(PubSubMessenger.name).log(Level.SEVERE, "Exception while publishing message", e);
        } finally {
            if (publisher != null) {
                publisher.shutdown();
                publisher.awaitTermination(1, TimeUnit.MINUTES);
            }
        }
    import java.nio.charset.StandardCharsets;
    import java.util.Base64;
    
    import com.google.cloud.functions.BackgroundFunction;
    import com.google.cloud.functions.Context;
    import PubSubMessage;

    
public class SendEmailFromSendGrid implements BackgroundFunction<PubSubMessage> {
    public SendEmailFromSendGrid() {
    }

    public void accept(PubSubMessage message, Context context) throws Exception {
        System.out.println("invoked accept");
        String name = "World";
        if (message != null && message.getData() != null) {
            name = new String(Base64.getDecoder().decode(message.getData().getBytes(StandardCharsets.UTF_8)),
                    StandardCharsets.UTF_8);
        }
        System.out.println(String.format("Hello %s!", name));
        return;
    }
}
导入java.nio.charset.StandardCharset;
导入java.util.Base64;
导入com.google.cloud.functions.BackgroundFunction;
导入com.google.cloud.functions.Context;
导入公共消息;
公共类SendEmailFromSendGrid实现BackgroundFunction{
公共SendEmailFromSendGrid(){
}
公共void接受(PubSubMessage消息、上下文)引发异常{
System.out.println(“调用接受”);
字符串name=“World”;
if(message!=null&&message.getData()!=null){
name=新字符串(Base64.getDecoder().decode(message.getData().getBytes(StandardCharsets.UTF_8)),
标准字符集(UTF_8);
}
System.out.println(String.format(“Hello%s!”,name));
返回;
}
}
PubSubMessage定义

    import java.util.Map;

    public class PubSubMessage {

    public static final String newUserLanguage = "userLanguage";

    private String data;
    private Map<String, String> attributes;
    private String messageId;
    private String publishTime;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, String> attributes) {
        this.attributes = attributes;
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public String getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(String publishTime) {
        this.publishTime = publishTime;
    }
}
import java.util.Map;
公共类发布子消息{
公共静态最终字符串newUserLanguage=“userLanguage”;
私有字符串数据;
私有地图属性;
私有字符串messageId;
私有字符串发布时间;
公共字符串getData(){
返回数据;
}
公共void setData(字符串数据){
这个数据=数据;
}
公共映射getAttributes(){
返回属性;
}
公共void集合属性(映射属性){
this.attributes=属性;
}
公共字符串getMessageId(){
返回messageId;
}
public void setMessageId(字符串messageId){
this.messageId=messageId;
}
公共字符串getPublishTime(){
返回出版时间;
}
public void setPublishTime(字符串publishTime){
this.publishTime=publishTime;
}
}

谢谢

此答案由@user1241724在评论部分提供:

重做了整个练习,它现在开始工作了。唯一的区别是 在PubSubMessage中添加了默认构造函数


你能分享你的代码来重现我们这边的最小示例吗?@guillaumeblaquiere pl现在在问题中找到了相同的结果。提前感谢。您是否打印了
消息.getData()
值?你确定还是b64格式吗?大多数客户端库自动转换为字符串内容。Hi@guillaumeblaquiere是第一个System.out.println(“调用接受”);它本身没有被调用。不确定在调用此accept函数之前是否进行了PubSub内部转换,并且在发送PubSub消息时丢失了一些格式无法复制。。。您还可以共享Java导入吗?(文件顶部)