2

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

我在 Jetty 9.0.6 上运行 Spring 3.2.4。

我有一个简单的控制器,方法如下

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

执行此请求时,我收到以下错误:

2013 年 10 月 31 日上午 10:16:16 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpRequestMethodNotSupported

警告:不支持请求方法“PUT”

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

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

编辑

原来我是愚蠢的。当他提到网址时,Affe 提示了我。我像 'api/employees?id=32' 一样访问它,而它应该是 'api/employees/32'

希望这对其他人有所帮助,这里是 Web 描述符、servlet 和控制器。

网页描述符

<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
@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);
    }
}
4

1 回答 1

3

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

我正在访问像'/api/employees?id=32'而不是'/api/employees/32'之类的url,尽管我认为参数绑定仍然会选择它。

于 2013-10-31T17:19:50.203 回答