Spring REST 控制器文件上传 - 请求方法 'POST' 不支持错误

Spring REST controller file upload - Request method 'POST' not supported error

我正在为 Web 应用程序创建原型,其中一个功能应该允许某人上传包含一些额外信息的 excel 文件。信息旁边的文件存储在一个对象中,该对象被序列化并存储。

我已经创建了上传方法,在尝试测试该函数时它抛出了 "Request method 'POST' not supported" 白标签错误页面。

我怀疑这可能是 pom.xml 的问题,但我不完全确定。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>project</groupId>
    <artifactId>answers</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>MVC 1.0 Blank Project (from https://github.com/making/mvc-1.0-blank)</name>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.ozark</groupId>
            <artifactId>ozark</artifactId>
            <version>1.0.0-m02</version>
        </dependency>
                <!-- Swing exercise -->
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout</artifactId>
            <version>3.5.5</version>
        </dependency>

        <!-- Web exercise -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>1.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.3.6.RELEASE</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这里是控制器class。我们将在一两天内转移到 Thymeleaf 配置,此 html 目前仅用于测试目的。



    @MultipartConfig
    @RestController
    @RequestMapping(value = "/Teacher", produces = "text/html;charset=UTF-8")
    public class Teacher {



        @GetMapping("")
        @ResponseBody
        TestController testcont;
        public String homePage(@RequestParam(value = "file", required = false) String name, HttpServletRequest request,
                HttpServletResponse response){

            StringBuilder sb = new StringBuilder();


            sb.append("<p> <a href='/Teacher/NewTest'>New Test upload</a></p>\n"
                    + "<p><a href='/SelectTest'>Select Test File</a> <button type='button'>Send Test</button></p>"
                    + "\n \n \n"
                    + "<p><a>Current Test for students:</a>\n <a href='/getCurrentTest'></a></p>"
                    );

            return sb.toString();
        }


        @GetMapping("/NewTest")
        @ResponseBody
        public String newTestUpload(HttpServletRequest request, HttpServletResponse response){
            StringBuilder sb = new StringBuilder();



            if(!request.getParameterNames().hasMoreElements()){
                sb.append("<p><form action='' method='post' enctype='multipart/form-data'>"
                        + "<label>Enter file</label><input type='file' name='file'>"
                        + "<button type='submit'>Upload</button></p>"

                        + "<p><form action='/testName'>Test Name: <input type='text' name='name' value=''></p>"

                        + "<p><form action='/addInfo'>Comment: <input type='text' comment='comment' value=''></p>"

                        + "<p>Answer 1: <input type='text' Answer='answer1' value=''></p>"

                        + "<p>Answer 2: <input type='text' Answer='answer2' value=''></p>"

                        + "</form>"

                        + "<a href='/Teacher'>Back</a>\n"
                        );
                return sb.toString();
            }
            else if(request.getParameter("file") != null && request.getParameter("name") != ""
                    && request.getParameter("comment") != "" && request.getParameter("answer1") != null
                    && request.getParameter("answer2") != null){

                try{
                    // Upload happens here
                    Part filePart = request.getPart("file");
                    String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
                    InputStream fileContent = filePart.getInputStream();

                    File testExcel = new File(fileName);
                    testExcel.createNewFile();

                    Files.copy(fileContent, Paths.get(testExcel.getName()), StandardCopyOption.REPLACE_EXISTING);


                    double ans1 = Double.parseDouble(request.getParameter("answer1"));
                    double ans2 = Double.parseDouble(request.getParameter("answer2"));

                    Test test = new Test(testExcel, request.getParameter("name"), 
                                        request.getParameter("comment"), ans1, ans2);

                    testcont.addTest(test);

                    sb.append("New test uploaded!<br/>\n<a href='/'>Back</a>\n");
                    return sb.toString();


                } catch (Exception e){
                    sb.append("<h1>Couldnt insert test</h1>");
                    response.setStatus(HttpServletResponse.SC_OK);
                    return sb.toString();
                }

            }
            else{
                sb.append("failed<br/>\n<a href='/'>Back</a>\n");
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                return sb.toString();
            }

        }

    }

控制器中的两种方法都有 @GetMapping 注释,这意味着它们支持 GET 请求。您需要将一个替换为 @PostMapping(以支持 POST 请求)

从您发布的控制器代码来看,您的两个端点都是 GET。 /老师:- @GetMapping /Teacher/NewTest :- @GetMapping

您正在尝试 POST 到不支持 POST 方法的端点,这就是您看到错误消息的原因,指定 'POST' 方法不受支持。

将您的方法更正为:

@PostMapping
@RequestMapping("/NewTest")
public String newTestUpload(HttpServletRequest request, HttpServletResponse response) {

@PostMapping(path = "/NewTest")
public String newTestUpload(HttpServletRequest request, HttpServletResponse response) {