Spring @RequestBody在映射POJO时导致不允许HTTP 405方法

时间:2015-01-06 01:04:08

标签: java spring rest

我最近一直在使用Spring构建一个RESTful API,而我却陷入了将请求主体映射到POJO的过程中。

为了清楚起见,我可能已经阅读了50个其他StackOverflow问题,说明了类似的症状,但无济于事。

使用@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;
  }
}

SimplePojo类:

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>

我无法发布任何内容类型的数据并让它成功。我尝试过表单JSON,表单数据和x-www-form-urlencoded。所有这些都导致405 Method Not Allowed错误。如果需要,我可以在此处发布请求详细信息。我正在使用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)

1 个答案:

答案 0 :(得分:1)

在请求中尝试此操作。

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