Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不支持Spring Boot 405 POST方法?_Java_Spring_Spring Boot - Fatal编程技术网

Java 不支持Spring Boot 405 POST方法?

Java 不支持Spring Boot 405 POST方法?,java,spring,spring-boot,Java,Spring,Spring Boot,Spring Boot MVC怎么可能不支持POST方法?!我试图实现一个简单的post方法,它接受一个实体列表:下面是我的代码 @RestController(value="/backoffice/tags") public class TagsController { @RequestMapping(value = "/add", method = RequestMethod.POST) public void add(@RequestBody List<Ta

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);
        }
}
请求机构:

[{"tagName":"qweqwe"},{"tagName":"zxczxczx"}]
我收到:

{
    "timestamp": 1441800482010,
    "status": 405,
    "error": "Method Not Allowed",
    "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
    "message": "Request method 'POST' not supported",
    "path": "/backoffice/tags/add"
} 
编辑

调试SpringWeb请求处理程序

     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");
        }
    }

supportedMethods
中仅有的两种方法是
{GET,HEAD}

RestController注释定义中存在错误。根据文件,它是:

公共@接口控制器{

/***该值可能表示对逻辑组件的建议 名称,*在自动检测到错误的情况下转换为SpringBean component.*@返回建议的组件名称(如果有)*@因为 4.0.1*/String value()默认为“”

}

这意味着您输入的值(“/backoffice/tags”)是控制器的名称,而不是其可用路径

在控制器的类上添加
@RequestMapping(“/backoffice/tags”)
,并从
@RestController
注释中删除值

编辑: 完全工作的示例,根据注释,它不工作-请尝试使用此代码-并从IDE本地运行

build.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

package demo;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.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();
    }
}
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);
    }
}
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);
    }
}
SampleController.java

package demo;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.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();
    }
}
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);
    }
}
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);
    }
}

实际上,当方法名可用但方法类型不同于所需类型时,此异常将抛出405。

我在开发时遇到了相同的错误。请检查URL路径是否正确映射,与您正在检查的路径相同。希望它能解决问题。

好的,这是一个问题,但它仍然不起作用。。。即使改变了它,也让我复制它。给我5分钟。更新TagsController的代码。考虑回滚到修订版2。现在,你问题中的代码是正确的,这使得答案毫无意义。@RafalG。所讨论的代码不应该是“固定的”,它打破了问题。