How I can call method using web client?

351 Views Asked by At

I wanna show new scene (JavaFX) when my post-request will be successfully completed, and show alerts when it will be 5xx/4xx errors. When i try to do this using WebFlux library, I getting error like "Bad return type in lambda expression: Optional cannot be converted to Mono<? extends Throwable>"

AuthController.java

    buttonAuth.setOnAction(
            actionEvent -> {
                UserAuth userAuth = UserAuth
                        .builder()
                        .login(loginAuth.getText())
                        .password(passAuth.getText())
                        .build();

                    webClient.post()
                            .uri("http://localhost:10120/message/authorization")
                            .contentType(MediaType.APPLICATION_JSON)
                            .bodyValue(userAuth)
                            .retrieve()
                            .onStatus(
                                    HttpStatus::is5xxServerError,
                                    response -> alertService.showAlert(Alert.AlertType.ERROR, "Error",
                                            "Server error", false))
                            .onStatus(
                                    HttpStatus::is4xxClientError,
                                    response -> alertService.showAlert(Alert.AlertType.ERROR, "Error",
                            "Client error", false))
                            .onStatus(
                                    HttpStatus.OK,
                                    response -> Platform.runLater(()-> {setScene(buttonAuth,"Main Menu", MainMenuController.class, fxWeaver);}))
                            .bodyToMono(Void.class)
                            .block();
              }
            });

UserAuth.java

import lombok.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Builder(toBuilder = true)
public class UserAuth {
    public String login;
    public String password;
}

AlertService.java

import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor
public class AlertService {
    public Optional<ButtonType> showAlert(Alert.AlertType type, String title, String content, boolean NeedToReturnResult) {
        Alert alert = new Alert(type);
        alert.setTitle(title);
        alert.setContentText(content);
        alert.setHeaderText(null);
        Optional<ButtonType> result = alert.showAndWait();
        if (NeedToReturnResult) return result;
        else {
            return null;
        }
    }
}

BaseController.java --- need to set scene when request successfully completed

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import net.rgielen.fxweaver.core.FxWeaver;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class BaseController {
    @Autowired
    private Stage stage;

    public void setScene(Button button, String title, Class controller, FxWeaver fxWeaver) {
        button.getScene().getWindow().hide();
        Parent root = (Parent) fxWeaver.loadView(controller);
        stage.setScene(new Scene(root));
        stage.setTitle(title);
        stage.setResizable(false);
        stage.show();

    }

    public void setScene(Hyperlink link, String title, Class controller, FxWeaver fxWeaver) {
        link.getScene().getWindow().hide();
        Parent root = (Parent) fxWeaver.loadView(controller);
        stage.setTitle(title);
        stage.setResizable(false);
        stage.setScene(new Scene(root));
        stage.show();

    }

    public void clearFields(TextField firstNameReg, TextField secondNameReg, TextField loginReg, PasswordField passReg, TextField mailReg) {
        firstNameReg.clear();
        secondNameReg.clear();
        loginReg.clear();
        passReg.clear();
        mailReg.clear();
    }

    public void clearFields(TextField login, PasswordField password) {
        login.clear();
        password.clear();
    }
}

Server-part -- here I tried to send some statuses when get request, but i don't know how to do that right

  @RequestMapping(
      value = "/authorization",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Flux<String> authorizationUser(@Valid UserDTO userDTO) {
    log.info("[UserController.authorizationUser] personDTO = {}", userDTO);
    log.info("[UserController.authorizationUser] personDTO = {}", userService.authorizationUser(userDTO).authorization);
    if(userService.authorizationUser(userDTO).authorization){
      return Flux.just("ok");
    }else{
      return Flux.just("bad_request");
    }
  }
0

There are 0 best solutions below