Wojtek Wencel
Wojtek Wencel

Reputation: 2117

How to forward form data to another host in Spring MVC Controller?

I have a Controller:

@Controller
public class ImageController {
    @GetMapping("/upload")
    public String uploadImageGet(Model model) {
        return "uploadForm";
    }
    @PostMapping("/upload")
    public String uploadImagePost(@ModelAttribute Image image, Model model) throws IOException {
        // what should I do here?
        return "result";
    }
}

An HTML form:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <form action="#"  th:action="@{/upload}" th:object="${image}" method="post" enctype="multipart/form-data">
        <label for="name">Name:</label><br>
        <input type="text" id="name" name="name" value=""><br>
        <label for="description">Description:</label><br>
        <input type="text" id="description" name="description" value=""><br>
        <label for="author">Author:</label><br>
        <input type="text" id="author" name="author" value=""><br>
        <label for="image">Image:</label><br>
        <input type="file" id="image" name="image"><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

And a class for storing the form data:

public class Image {
    private String name;
    private String author;
    private String description;
    private MultipartFile image;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public MultipartFile getImage() {
        return image;
    }

    public void setImage(MultipartFile image) {
        this.image = image;
    }
}

I want to send the form data to another host (Spring API), but also display a "Uploaded successfully" response page. Because of that I wanted to handle all that in the controller, but can't figure out how to do this. I was able to find some resources on creating a request manually, but this seems like there must be some simpler way to do this. If I'm approaching it wrong please let me know.

Upvotes: 0

Views: 635

Answers (1)

Forgotten Dreamer
Forgotten Dreamer

Reputation: 69

It hardly depends what you mean by:

I want to send the form data to another host (Spring API)

One approach would be to send the Data async with REST to another Spring application or any other application using springs rest template: https://www.baeldung.com/rest-template.

And show the user a succesfull html site.

But if you do it async you should keep in mind that the REST call could fail but you showed the user a successfull page..

My recommendation would be to create an additional REST Endpoint in your spring boot application and send the data from the site with an fetch or ajax call via javascript to it, from where it could be sent to the other spring application

Upvotes: 1

Related Questions