Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

java - 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.

question from:https://stackoverflow.com/questions/65918598/how-to-forward-form-data-to-another-host-in-spring-mvc-controller

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...