AWS SNS使用Spring Cloud发送。试图理解

时间:2015-06-19 19:20:56

标签: java spring amazon-sns spring-cloud

我是春天的新手。我正在尝试通过练习为我的iOS应用程序实现简单的推送服务器以及GitHub上的文档及其示例。但我做错了什么,我无法理解。请问有人帮帮我吗?

所以我的SnsSenderController代码是:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController                
@RequestMapping("/sns")        
public class SnsSenderController {      

    private static final Logger theLogger = LoggerFactory.getLogger(SnsSenderController.class);
    private final NotificationMessagingTemplate notificationMessagingTemplate;

    @Autowired
    public SnsSenderController(NotificationMessagingTemplate notificationMessagingTemplate) {
        this.notificationMessagingTemplate = notificationMessagingTemplate;
    }

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public void sendNotification(@RequestBody SnsNotification notification) {
        theLogger.debug("Going to send notification {}", notification);

        this.notificationMessagingTemplate.sendNotification("SnsTopic", notification.getMessage(), notification.getSubject());
    }

}

我的SnsNotification类:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class SnsNotification {

    private final String subject;
    private final String message;

    @JsonCreator
    public SnsNotification(@JsonProperty("subject") String subject, @JsonProperty("message") String message) {
        this.subject = subject;
        this.message = message;
    }

    public String getSubject() {
        return this.subject;
    }

    public String getMessage() {
        return this.message;
    }

    @Override
    public String toString() {
        return "SnsNotification{" + "subject='" + this.subject + '\'' + ", message='" + this.message + '\'' + '}';
    }

}

豆类:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
       xmlns:aws-messaging="http://www.springframework.org/schema/cloud/aws/messaging"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/cloud/aws/context
                           http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context-1.0.xsd
                           http://www.springframework.org/schema/cloud/aws/messaging
                           http://www.springframework.org/schema/cloud/aws/messaging/spring-cloud-aws-messaging-1.0.xsd">

       <!-- Define global credentials for all the AWS clients -->
       <aws-context:context-credentials>
              <aws-context:instance-profile-credentials/>
              <aws-context:simple-credentials access-key="${accessKey}"
                                              secret-key="${secretKey}"/>
       </aws-context:context-credentials>

       <!-- Messaging-->
       <aws-messaging:notification-messaging-template id="notificationMessagingTemplate" />

</beans>

主要的应用代码是:

import com.amazonaws.services.sns.AmazonSNS;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.aws.core.env.ResourceIdResolver;
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration

public class MySenderApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySenderApplication.class, args);
    }

    @Bean
    public NotificationMessagingTemplate notificationMessagingTemplate(AmazonSNS amazonSNS, ResourceIdResolver resourceIdResolver) {
        return new NotificationMessagingTemplate(amazonSNS, resourceIdResolver);
    }
}

从很多错误开始就崩溃了。 请!

更新

我不知道为什么,但它开始工作......然后还有两个问题。

  1. 根据Spring Cloud Docs,AWS的连接设置应该在application.properties中。我用密钥cloud.aws.credentials.accessKey和cloud.aws.credentials.secretKey把它放在那里但是那里有一个错误:“无法解析配置属性”。为什么这样?我错过了什么?
  2. 根据Spring Docs,我可以在那里制作“notificationMessagingTemplate”而不是注释@Bean,但我的代码只能在代码中使用@Bean注释。为什么会这样?

0 个答案:

没有答案