I am newbie in Spring. I am trying to learn it through practice with implementing simple push server for my iOS app with documentation and their samples on GitHub. But I do something wrong and I cannot understand what. Could somebody help me, please?
So my SnsSenderController code is:
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());
}
}
My SnsNotification class:
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 + '\'' + '}';
}
}
Beans:
<?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>
The main app code is:
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);
}
}
And it is crash by start with a lot of errors. Please!
UPDATE:
I don't know why, but it starts work... Then two more questions.
- According to Spring Cloud Docs the connection settings to AWS should be in application.properties. I put it there with keys cloud.aws.credentials.accessKey and cloud.aws.credentials.secretKey but there is an error there: "Cannot resolve configuration property". Why so? What did I miss?
- According to Spring Docs I can make "notificationMessagingTemplate" there instead of annotation @Bean, but my code works only with @Bean annotation in code. Why so?