Spring @RequestBody 在映射 POJO 时导致 HTTP 405 Method Not Allowed

Spring @RequestBody causes HTTP 405 Method Not Allowed when mapping POJO

我最近一直在使用 Spring 构建一个 RESTful API,但我被困在将请求主体映射到 POJO 中。

需要说明的是,我可能已经阅读了其他 50 个 Whosebug 问题,这些问题说明了类似的症状,但无济于事。

使用 @RequestBody 注释,我的控制器可以成功地将请求主体映射到 String。但是,当使用 POJO 时,Spring 抱怨 o.s.web.servlet.PageNotFound : Request method 'POST' not supported.

控制器:

package api.controller;

import api.domain.SimplePOJO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TestController {
  @RequestMapping(value = "/test", method = RequestMethod.POST)
  public SimplePOJO testing(@RequestBody SimplePOJO simplePOJO) {
    log.info("Made it into the testing method.");
    return simplePOJO;
  }
}

简单POJO:

package api.domain;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class SimplePOJO {
  private String name;

  private Integer age;
}

我的应用程序配置也不是那么复杂。也许它缺少什么?

package api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.hateoas.config.EnableHypermediaSupport;

@ComponentScan
@EnableAutoConfiguration
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

我的应用程序依赖项中可能还缺少一些东西,所以附上我的 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">
    <properties>
    </properties>

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <groupId>api</groupId>
    <artifactId>API</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId>
            <version>1.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.14.8</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava-collections</artifactId>
            <version>r03</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我无法 POST 任何内容类型的数据并使其成功。我已经尝试了 JSON、form-data 和 x-www-form-urlencoded 形式。所有这些都会导致 405 Method Not Allowed 错误。如果需要,我可以在此处 post 请求详细信息。我正在使用 Postman 进行测试,所以我相当确定请求的格式正确。

当我启动应用程序时,我在控制台中看到 s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public api.domain.SimplePOJO api.controller.TestController.testing(api.domain.SimplePOJO)

在请求中试试这个。

{ "name":"shazin", "age":28 }