[英]Spring @RequestBody causes HTTP 405 Method Not Allowed when mapping POJO
I've been building a RESTful API using Spring recently and I'm stuck at the point of mapping request bodies into POJOs. 我最近一直在使用Spring构建一个RESTful API,但我一直停留在将请求主体映射到POJO中。
To be clear, I have probably read 50 other StackOverflow questions stating similar symptoms but to no avail. 需要明确的是,我可能已经阅读了50个其他StackOverflow问题,这些问题都说明了类似的症状,但无济于事。
Using the @RequestBody
annotation, my controller can successfully map the request body to a String
. 使用
@RequestBody
批注,我的控制器可以成功地将请求主体映射到String
。 When using POJOs however, Spring complains that osweb.servlet.PageNotFound : Request method 'POST' not supported
. 但是,当使用POJO时,Spring抱怨
osweb.servlet.PageNotFound : Request method 'POST' not supported
。
Controller: 控制器:
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: SimplePojo类:
package api.domain;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class SimplePOJO {
private String name;
private Integer age;
}
My application configuration is not all that complicated, either. 我的应用程序配置也不是那么复杂。 Perhaps it is missing something?
也许它缺少什么?
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);
}
}
I may also be missing something from my application dependencies, so my pom.xml is attached: 我的应用程序依赖项中可能还缺少一些内容,因此附加了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>
I cannot POST data of any content type and have it succeed. 我无法发布任何内容类型的数据,并且无法成功。 I have tried the forms JSON, form-data, and x-www-form-urlencoded.
我已经尝试过JSON,表单数据和x-www-form-urlencoded表单。 All lead to the 405 Method Not Allowed error.
全部导致405方法不允许错误。 If needed, I can post the request details here.
如果需要,我可以在此处发布请求详细信息。 I am using Postman for testing so I am fairly certain that the request is well formed.
我正在使用Postman进行测试,因此可以确定请求的格式是否正确。
When I start the application, I see swsmmaRequestMappingHandlerMapping : Mapped "{[/test],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public api.domain.SimplePOJO api.controller.TestController.testing(api.domain.SimplePOJO)
in the console. 启动应用程序时,我看到
swsmmaRequestMappingHandlerMapping : Mapped "{[/test],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public api.domain.SimplePOJO api.controller.TestController.testing(api.domain.SimplePOJO)
控制台中的swsmmaRequestMappingHandlerMapping : 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 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.