遇到此错误
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpRequestMethodNotSupported WARNING:
我尝试了get方法,但它正在工作。
我的控制器
package com.demoapp.demo.controller;
import com.demoapp.demo.manager.UserManager;
import com.demoapp.demo.model.error.WebServiceError;
import com.demoapp.demo.model.error.WebServiceException;
import com.demoapp.demo.model.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
UserManager userManager;
@RequestMapping(value = "", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
@ResponseBody
public User addUser(@RequestBody User user) throws Exception {
return user;
// return userManager.add(user);
}
@RequestMapping(value = "/", method = RequestMethod.PUT, consumes = "application/json", produces = "application/json")
@ResponseBody
public String updateUser() throws Exception {
return "hello";
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public User fetchUser(@PathVariable Long id) {
return userManager.fetch(id);
}
@RequestMapping(value = "/username/{username}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public User fetchUserByUsername(@PathVariable String username) {
return userManager.fetchByUsername(username);
}
@RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public List<User> fetchAllUsers(@RequestParam(value = "include_all", required = false) boolean includeAll) {
return userManager.fetchAll(includeAll);
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "application/json")
@ResponseBody
public User deleteUser(@PathVariable Long id, @RequestBody User user) throws Exception {
return userManager.delete(id, user);
}
// Exception handler for WebServiceException cases
@ExceptionHandler(WebServiceException.class)
public ResponseEntity<WebServiceError> handleWebServiceException(WebServiceException webServiceException) {
System.out.println("UserController.handleWebServiceException");
WebServiceError webServiceError = new WebServiceError(webServiceException.getExceptionCode(), webServiceException.getExceptionMessage());
// This should happen in case of input constraint validations
if(webServiceException.getExceptionMessageList() != null) {
webServiceError.setErrorMessageList(webServiceException.getExceptionMessageList());
}
return new ResponseEntity<WebServiceError>(webServiceError, HttpStatus.BAD_REQUEST);
}
// Exception handler for generic Exception cases
/* @ExceptionHandler(Exception.class)
public ResponseEntity<WebServiceError> handleException(Exception exception) {
System.out.println("UserController.handleException");
WebServiceError webServiceError = new WebServiceError(400, exception.getMessage());
return new ResponseEntity<WebServiceError>(webServiceError, HttpStatus.BAD_REQUEST);
}*/
}
WEB.XML文件
<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_2_5.xsd"
version="2.5">
<display-name>Web Application</display-name>
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>com.demoapp.demo.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
答案 0 :(得分:0)
在spring-servlet.xml中添加这两个..
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
根据您的Dispatcher Servlet Mapping(/),将控制器顶部的RequestMapping更改为......
@Controller
@RequestMapping("/")
public class UserController {