gpt4 book ai didi

java - Spring MVC : HTTP 405 - Request method 'GET' not supported when making POST request

转载 作者:行者123 更新时间:2023-11-28 22:02:46 25 4
gpt4 key购买 nike

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

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

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>

Jackson 在 pom.xml 中声明为依赖项。

当我发出 POST 请求(通过 Postman)时,tomcat 返回 HTTP 405 - Request method 'GET' not supported。有什么配置可以让tomcat响应POST请求吗?

最佳答案

尝试使用

curl -X POST http://localhost:8080

检查。

或者尝试为您的测试设置另一个 url,而不是 '/'。

关于java - Spring MVC : HTTP 405 - Request method 'GET' not supported when making POST request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24159136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com