Java 不支持 Spring Boot 405 POST 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32479919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot 405 POST method not supported?
提问by Adelin
How could POST method not support by Spring Boot MVC ?! I am trying to implement a simple post method that accepts a list of entities : here is my code
Spring Boot MVC 怎么可能不支持 POST 方法?!我正在尝试实现一个接受实体列表的简单 post 方法:这是我的代码
@RestController(value="/backoffice/tags")
public class TagsController {
@RequestMapping(value = "/add", method = RequestMethod.POST)
public void add(@RequestBody List<Tag> keywords) {
tagsService.add(keywords);
}
}
Hitting this URL like this:
像这样点击这个网址:
http://localhost:8090/backoffice/tags/add
Request Body:
请求正文:
[{"tagName":"qweqwe"},{"tagName":"zxczxczx"}]
I receive:
我收到:
{
"timestamp": 1441800482010,
"status": 405,
"error": "Method Not Allowed",
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'POST' not supported",
"path": "/backoffice/tags/add"
}
EDIT:
编辑:
Debugging Spring Web Request Handler
调试 Spring Web 请求处理程序
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.checkRequest(request);
protected final void checkRequest(HttpServletRequest request) throws ServletException {
String method = request.getMethod();
if(this.supportedMethods != null && !this.supportedMethods.contains(method)) {
throw new HttpRequestMethodNotSupportedException(method, StringUtils.toStringArray(this.supportedMethods));
} else if(this.requireSession && request.getSession(false) == null) {
throw new HttpSessionRequiredException("Pre-existing session required but none found");
}
}
The only two methods in supportedMethods
are {GET,HEAD}
中仅有的两种方法supportedMethods
是{GET,HEAD}
采纳答案by Rafal G.
You have an error in RestController annotation definition. According to the docs it is:
您在 RestController 注释定义中有错误。根据文档,它是:
public @interface RestController {
/** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any * @since 4.0.1 */ String value() default "";
}
公共@interface RestController {
/** * 该值可能表示对逻辑组件名称的建议,* 在自动检测到的组件的情况下将其转换为 Spring bean。* @return 建议的组件名称,如果有的话 * @since 4.0.1 */ String value() default "";
}
Which means the value you have entered ("/backoffice/tags") is NAME of the controller not the path under which it is available.
这意味着您输入的值(“/backoffice/tags”)是控制器的名称,而不是可用的路径。
Add @RequestMapping("/backoffice/tags")
on the controller's class and remove value from the @RestController
annotation.
添加@RequestMapping("/backoffice/tags")
控制器的类并从@RestController
注释中删除值。
EDIT:Fully working example as per comment that it does not work - try to use this code please - and run locally from IDE.
编辑:根据注释完全工作的示例,它不起作用 - 请尝试使用此代码 - 并从 IDE 本地运行。
build.gradle
构建.gradle
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'demo'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
Tag.java
标签.java
package demo;
import com.fasterxml.Hymanson.annotation.JsonCreator;
import com.fasterxml.Hymanson.annotation.JsonProperty;
public class Tag {
private final String tagName;
@JsonCreator
public Tag(@JsonProperty("tagName") String tagName) {
this.tagName = tagName;
}
public String getTagName() {
return tagName;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Tag{");
sb.append("tagName='").append(tagName).append('\'');
sb.append('}');
return sb.toString();
}
}
SampleController.java
示例控制器.java
package demo;
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;
import java.util.List;
@RestController
@RequestMapping("/backoffice/tags")
public class SampleController {
@RequestMapping(value = "/add", method = RequestMethod.POST)
public void add(@RequestBody List<Tag> tags) {
System.out.println(tags);
}
}
DemoApplication.java
演示应用程序.java
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
回答by Vipul Kacha
Actually When method name is available but method type is different than required type so this exception will throw 405.
实际上,当方法名称可用但方法类型与所需类型不同时,此异常将抛出 405。
回答by Venkatanarayana Chittela
I faced the same kind of error when I was developing. Please check the URL path is correctly mapped with the same as you are checking. Hope it resolves the problem.
我在开发时遇到了同样的错误。请检查 URL 路径是否正确映射,与您正在检查的相同。希望它能解决问题。