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

时间:2019-07-18 09:57:43

标签: spring spring-restcontroller

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

我已经创建了上载方法,并且在尝试测试该功能时,它抛出了“不支持请求方法'POST'”白标签错误页面。

我怀疑可能是pom.xml有问题,但我不确定。

SELECT * FROM Student 
WHERE GraduationDate 
BETWEEN
(CASE
     WHEN (GraduationDate  Is Not Null)
     THEN SELECT convert(datetime, (?) , 121)
     ELSE''
END)
AND
(CASE
     WHEN (GraduationDate  Is Not Null)
     THEN SELECT convert(datetime, (?) , 121)
     ELSE''
END)

这是Controller类。我们将在一两天内迁移到Thymeleaf配置,此html仅用于测试目的。

<?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>

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

您正尝试将其发布到不支持POST方法的端点,这就是为什么您看到错误消息,并指定了不支持的'POST'方法。

答案 2 :(得分:1)

将您的方法更正为:

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

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