gpt4 book ai didi

java - SpringMVC 3.2.4 不支持请求方法 'PUT'

转载 作者:行者123 更新时间:2023-11-30 08:29:06 24 4
gpt4 key购买 nike

刚开始使用 Java 和 Spring,来自 C# 背景,但我无法让“PUT”请求正常工作。

我在 Jetty 9.0.6 上运行的 Spring 3.2.4。

我有一个简单的 Controller ,方法如下

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ResponseEntity<String> update(@PathVariable Integer id, @RequestBody Employee employee) {

HttpHeaders headers = new HttpHeaders();
headers.set("content-location", "/api/employees/" + id);

return new ResponseEntity<>("", headers, HttpStatus.OK);
}

执行此请求时,出现以下错误:

Oct 31, 2013 10:16:16 AM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpRequestMethodNotSupported

WARNING: Request method 'PUT' not supported

如果我将 RequestMethod 更改为“POST”,它会正常工作。

Spring 是否支持“PUT”请求?我如何让它识别“PUT”

编辑

原来我是个傻子。当 Affe 提到 url 时,他告诉了我。当它应该是“api/employees/32”时,我像“api/employees?id=32”一样访问它

希望这对其他人有帮助,这是网络描述符、servlet 和 Controller 。

网络描述符

<web-app xmlns='http://java.sun.com/xml/ns/javaee'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd'
version='3.0'>
<display-name>timesheet-app</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

MVC-Dispatcher-Servlet

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

<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package='org.timesheets.web' />

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

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

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class='org.springframework.web.servlet.view.InternalResourceViewResolver'>
<property name='prefix' value='/WEB-INF/views/' />
<property name='suffix' value='.jsp' />
</bean>
</beans>

Controller

@Controller
@RequestMapping("/api/employees")
public class EmployeeController {

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody List<Employee> get() {

List<Employee> employees = new ArrayList<>();

for(int i = 1; i <= 10; i++){
employees.add(new Employee(i, "Test " + i, "Department " + i));
}

return employees;
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public @ResponseBody Employee get(@PathVariable Integer id) {

return new Employee(1, "Test", "IT");
}

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> create(@RequestBody Employee employee) {

HttpHeaders headers = new HttpHeaders();
headers.set("content-location", "/api/employees/32");

return new ResponseEntity<>("", headers, HttpStatus.CREATED);
}

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ResponseEntity<String> update(@PathVariable Integer id, @RequestBody Employee employee) {

HttpHeaders headers = new HttpHeaders();
headers.set("content-location", "/api/employees/" + id);

return new ResponseEntity<>("", headers, HttpStatus.OK);
}
}

最佳答案

问题正如@Affe 指出的那样,它无法在映射中找到 url,因为我请求了错误的 url!

我正在访问像“/api/employees?id=32”而不是“/api/employees/32”这样的 url,尽管我本以为参数绑定(bind)仍会拾取它。

关于java - SpringMVC 3.2.4 不支持请求方法 'PUT',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19710525/

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