I am working on a little project, that sends emails automaticaly to an emailadress.
I am using Spring Boot, with Spring Security.
Basically I have this class:
package com.birthdayz.web;
import com.birthdayz.domain.service.MailSendingService;
import com.birthdayz.domain.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.reactive.function.client.WebClient;
import com.birthdayz.domain.model.User;
@Controller
public class MailController {
@Autowired
MailSendingService mailSendingService;
@Autowired
UserService userService;
@GetMapping("/send")
public void sendMail(OAuth2AuthenticationToken auth) {
System.out.println("request to /send success");
User loggedUser = userService.findUserBy(auth.getPrincipal().getAttribute("login"));
if (loggedUser == null) {
System.err.println("Fehler: Kein Benutzer in der Session gefunden.");
}
mailSendingService.sendMail(loggedUser);
}
@Scheduled(fixedRate = 5000)
public void sendRequest() {
WebClient.create()
.get()
.uri(uriBuilder -> uriBuilder
.scheme("http")
.host("localhost")
.port(8080)
.path("/send")
.build())
.retrieve()
.bodyToFlux(String.class)
.doOnError(error -> System.err.println("Fehler beim Abrufen der URL: " + error.getMessage()))
.doOnNext(response -> System.out.println("Antwort erhalten: " + response))
.subscribe();
}
}
My Idea is that I just simply schedule a GET-Request to /send every x seconds. But it doesnt work and I have tried building up the webClient many ways.
When I go to the browser http://localhost:8080/send, the email gets send away like wished.
Can somebody help me udnerstand this problem?