如何在@PostMapping中返回值

时间:2019-05-04 13:38:54

标签: java spring spring-boot

我在Spring Boot中是一个新手,遇到了一些问题。 我有汽车和文件,当我添加汽车数据并将其保存在db中时,要上传通过关系OneToOne连接到汽车的文件,但是当将汽车保存在@PostMapping(“ / saveAutomobile”)中时,我无法使用此AutomobileId并将其传输到@PostMapping(“ / uploadFile”)进行连接。你有什么建议吗?

添加汽车:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<div th:replace="fragments/links"></div>
<meta charset="UTF-8" />
<title>New Brand</title>
<link th:href="@{resources/css/bootstrap.min.css}" rel="stylesheet"></link>
</head>
<body>
    <div class="container add shadow">
        <h2>New Automobile</h2>
        <form th:action="@{/automobile/saveAutomobile}" method="post"
            th:object="${automobileForm}">
            <div class="row">
                <div class="column">
                    <div class="form-group shadow">
                        <label class="form-control-label" for="inputBrand"> Brand</label>
                        <input type="text"
                            class="form-control form-control-danger box-shadow"
                            id="inputBrand" th:field="*{brand}" name="brand"
                            required="required" />
                    </div>
                    <div class="form-group shadow">
                        <label class="form-control-label" for="inputModel"> Model</label>
                        <input type="text"
                            class="form-control form-control-danger box-shadow"
                            id="inputModel" th:field="*{model}"  name="model"/>

                </div>
            </div>
            <div style="text-align: center;">
                <a th:href="@{/file/uploadFile} + ${automobile?.getId()}"> <input type="submit"
                    class="btn btn-default box-shadow shadow" />
                </a>
            </div>
        </form>
    </div>
    <script th:src="@{resources/js/jquery-1.11.1.min.js}"></script>
    <script th:src="@{resources/js/bootstrap.min.js}"></script>
</body>
</html>
@PostMapping("/saveAutomobile")
    public String saveAutomobile(@Valid final AutomobileForm automobileForm, final BindingResult result) {

        if (result.hasErrors()) {
            LOG.error("SaveAutomobile Error: " + result);

            return "automobile/add";
        }

        final Automobile automobile = automobileConverter.ConvertToEntity(automobileForm);
        if (LOG.isDebugEnabled()) {
            LOG.info("Saving client " + automobile);
        }

        automobileService.save(automobile);
        automobileForm.setId(automobile.getId());
        return "/files/uploadFile";
    }

上传文件:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<div th:replace="fragments/links"></div>
<meta charset="UTF-8" />
<meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0" />

<title>Upload automobile documentation</title>
<link th:href="@{resources/css/bootstrap.min.css}" rel="stylesheet"></link>
</head>
<body>
    <div class="container upload shadow">
            <h2>Upload automobile documentation</h2>
            <form th:action="@{/uploadFile}" method="post"
                enctype="multipart/form-data" id="singleUploadForm"
                name="singleUploadForm" >
                <div class="form-group shadow">
                    <label class="form-control-label" for="uploadfile">Upload
                        File:</label> <input id="singleFileUploadInput" type="file" name="file"
                        class="file-input form-control form-control-danger box-shadow" />
                </div>
                <button type="submit" class="btn btn-default box-shadow shadow"
                    id="btnSubmit">Upload</button>
            </form>
            <div class="upload-response">
                <div id="singleFileUploadError"></div>
                <div id="singleFileUploadSuccess"></div>
            </div>
        </div>
    <script src="/js/main.js"></script>
</body>
</html>
@Autowired
    private FileStorageService DBFileStorageService;

    @PostMapping("/uploadFile")
    public FileForm uploadFile(@RequestParam("file") MultipartFile file) {
        File dbFile = DBFileStorageService.storeFile(file);

        return new FileForm(dbFile.getFileName(),
                file.getContentType(), file.getSize());
    }

2 个答案:

答案 0 :(得分:0)

您可以使用模型来存储数据并在HTML页面中使用它 例如:-

Controller.java

    @GetMapping("/goToViewPage")
    public ModelAndView passParametersWithModelAndView() {
        ModelAndView modelAndView = new ModelAndView("viewPage");
        modelAndView.addObject("message", "Hello from the controller");
        return modelAndView;
    }

Test.html

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Title</title>
    </head>
    <body>
        <div>Web Application. Passed parameter : th:text="${message}"</div>
    </body>
    </html>

答案 1 :(得分:0)

@PostMapping("/uploadFile")
public ResponseEntity<FileForm> uploadFile(@RequestParam("file") MultipartFile file) {
    File dbFile = DBFileStorageService.storeFile(file);
    FileForm fileForm = new FileForm(dbFile.getFileName(), file.getContentType(), file.getSize());

    return new ResponseEntity<>(fileForm, HttpStatus.OK);
}