Spring MVC:HTTP 405 - 发出POST请求时不支持请求方法'GET'

时间:2014-06-11 09:21:02

标签: java spring maven tomcat spring-mvc

刚刚启动了一个默认的Spring MVC项目,我无法让POST请求处理程序工作。这是我的控制者:

package com.myapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myapp.domain.Response;

@Controller
public class MyAppController {

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Response query() {
        return new Response();
    }
}

响应是一个简单的POJO:

package com.myapp.domain;

public class Response {

    private String text;
    private String ref;

    public Response() {}

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }
}

servlet的context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.myapp" />



</beans:beans>

杰克逊被宣布为pom.xml中的依赖。

当我发出POST请求时(通过Postman),tomcat返回HTTP 405 - Request method 'GET' not supported。是否有任何配置让tomcat响应POST请求?

1 个答案:

答案 0 :(得分:0)

尝试使用

curl -X POST http://localhost:8080

检查。

或尝试为您的测试设置另一个网址,而不是&#39; /&#39;。