requestmapping-spring mvc method 设置无效

问题描述

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这个请求的,正常情况下会报错如下的。

时间: 2024-12-24 21:40:19

requestmapping-spring mvc method 设置无效的相关文章

java spring mvc怎么设置全局增删改方法为post请求

问题描述 java spring mvc怎么设置全局增删改方法为post请求 我不想在每个增删改方法注解的形式表明此方法为post请求,想在配置文件中设置全局的以add*updata*等开头的方法为post请求 解决方案 @Controller@RequestMapping(value=""t""method=RequestMethod.POST)public class TestController {...} 解决方案二: @Controller@Request

基于注解的spring mvc 怎么设置默认的调用方法

问题描述 就是类似ROR中的功能,如果没有指定方法,则调用index方法.假如在springmvc中,没有指定method,系统就会报错,请问有没有设置?或者说只能指定方法@RequestMapping(params="method=list")

Spring MVC 中的@RequestMapping(method = RequestMethod.OPTIONS)无法拦截

问题描述 Spring MVC 中的@RequestMapping(method = RequestMethod.OPTIONS)无法拦截,貌似这个注解不能拦截OPTIONS方法,get,post,put都能,为什么这个不能,有什么解决办法 解决方案 这里给个提示,看是否解决:http://stackoverflow.com/questions/23103832/spring-mvc-does-not-handle-requestmethod-options

spring mvc设置应答体的content type

spring MVC中如何设置应答体的content type呢? spring MVC中如何设置返回类型呢? 我们知道response 的content type主要有: text/html text/plain application/json;charset=UTF-8 application/octet-stream 等 先举一个例子,spring mvc中可以通过如下方式返回json字符串: Java代码   @ResponseBody       @RequestMapping(va

Spring MVC 基础注解之@RequestMapping、@Controller、(二)

我现在学的是spring4.2 今天主要学习了Spring MVC注解  引入注解可以减少我们的代码量,优化我们的代码. @Controller:用于标识是处理器类: @RequestMapping:请求到处理器功能方法的映射规则: 还是以示例来解释说明   1 创建springAnnotation02项目,导入jar包.    这里的web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app x

Spring MVC 基于Method的映射规则(注解版)

在Restful风格的web开发中,根据不同的请求方法使用相应的控制器处理逻辑成为核心需求,下面就看看如何在Spring MVC中识别不同的请求方法. 请求方法 在Http中,请求的方法有很多种,最常见的就是GET.POST,他们的差异就不过多赘述了.由于Restful概念的兴起,即使用Url的不同请求方法来控制业务方法,很多请求方法都开始流行起来,比如PUT.DELETE等等. 那么就先介绍下各个请求方法的使用场景吧! GET 平时网页的一些基本的URL都是GET请求的,用于执行查询操作. 但

Supported method argument types Spring MVC

Supported method argument types The following are the supported method arguments: Request or response objects (Servlet API). Choose any specific request or response type, for example ServletRequest or HttpServletRequest. Session object (Servlet API):

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestPar

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable Pankaj July 4, 2014 Spring @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotat

spring mvc开发公众号-spring mvc开发的微信公众号怎么设置验证时的URL

问题描述 spring mvc开发的微信公众号怎么设置验证时的URL 您好. 我想咨询一下用spring mvc开发的微信公众号,在填写服务器配置时的url时应该写哪些?我的项目名是:weixinGo RequestMapping是valid.do 解决方案 就写上域名/valid.do就行,然后把你这个应用的token写上,在把剩下的填完就行了 解决方案二: http://域名/valid.do/? 这样吗?