问题描述
- spring mvc method 设置无效
-
@Controller
@RequestMapping("/travel/api/user")
public class UserController {
@RequestMapping(value="/register",method=RequestMethod.POST)
public @ResponseBody String register()
{
return "register";
}
}请问这种接口,我明明设置了method=RequestMethod.POST为啥通过GET方式请求居然可以返回呢?是不是需要有特别的配置呢?Spring-mvc 4.1.6版本下
解决方案
这个问题有点难度。。。。
解决方案二:
你打算让spring做什么呢,spring源码中对是否是get的处理没怎么做判断,另外你是用
org.springframework.web.servlet.DispatcherServlet
在web启动时扫描可适配的handler类class的吧。
protected void doDispatch(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
HttpServletRequest processedRequest;
HandlerExecutionChain mappedHandler;
int interceptorIndex;
processedRequest = request;
mappedHandler = null;
interceptorIndex = -1;
boolean errorView = false;
processedRequest = checkMultipart(request);
mappedHandler = getHandler(processedRequest, false);
if (mappedHandler != null && mappedHandler.getHandler() != null) goto _L2; else goto _L1
_L1:
noHandlerFound(processedRequest, response);
_L8:
if (processedRequest != request)
cleanupMultipart(processedRequest);
return;
_L2:
HandlerAdapter ha;
boolean isGet;
long lastModified;
ha = getHandlerAdapter(mappedHandler.getHandler());
String method = request.getMethod();
isGet = "GET".equals(method);
if (!isGet && !"HEAD".equals(method))
break MISSING_BLOCK_LABEL_199;
lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (logger.isDebugEnabled())
{
String requestUri = urlPathHelper.getRequestUri(request);
logger.debug((new StringBuilder("Last-Modified value for [")).append(requestUri).append("] is: ").append(lastModified).toString());
}
if ((new ServletWebRequest(request, response)).checkNotModified(lastModified) && isGet)
continue; /* Loop/switch isn't completed /
HandlerInterceptor interceptors[] = mappedHandler.getInterceptors();
if (interceptors == null) goto _L4; else goto _L3
_L3:
int i = 0;
goto _L5
_L6:
HandlerInterceptor interceptor = interceptors[i];
if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler()))
{
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
continue; / Loop/switch isn't completed */
}
interceptorIndex = i;
i++;
_L5:
if (i < interceptors.length) goto _L6; else goto _L4
_L4:
ModelAndView mv;
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (mv != null && !mv.hasView())
mv.setViewName(getDefaultViewName(request));
if (interceptors != null)
for (i = interceptors.length - 1; i >= 0; i--)
{
interceptor = interceptors[i];
interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);
}
break MISSING_BLOCK_LABEL_426;
ModelAndViewDefiningException ex;
ex;
logger.debug("ModelAndViewDefiningException encountered", ex);
mv = ex.getModelAndView();
break MISSING_BLOCK_LABEL_426;
ex;
Object handler = mappedHandler == null ? null : mappedHandler.getHandler();
mv = processHandlerException(processedRequest, response, handler, ex);
errorView = mv != null;
if (mv != null && !mv.wasCleared())
{
render(mv, processedRequest, response);
if (errorView)
WebUtils.clearErrorRequestAttributes(request);
} else
if (logger.isDebugEnabled())
logger.debug((new StringBuilder("Null ModelAndView returned to DispatcherServlet with name '")).append(getServletName()).append("': assuming HandlerAdapter completed request handling").toString());
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
break MISSING_BLOCK_LABEL_583;
Exception ex;
ex;
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
throw ex;
Error err;
err;
ServletException ex = new NestedServletException("Handler processing failed", err);
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
throw ex;
Exception exception;
exception;
if (processedRequest != request)
cleanupMultipart(processedRequest);
throw exception;
if (processedRequest != request)
cleanupMultipart(processedRequest);
return;
if (true) goto _L8; else goto _L7
_L7:
}
解决方案三:
就是这样的用法。你确定你访问这个全路径的请求用的是GET方法?调试一下是不是进入这个方法里面了。
你能用GET访问的肯定不是/travel/api/user/register这个请求的,正常情况下会报错如下的。