在Spring MVC Controller中使用CookieParam注释

时间:2018-05-10 19:36:39

标签: java spring spring-mvc

我正在尝试使用Actor注释从HTTP请求中获取cookie到我控制器上的方法。

javax.ws.rs.CookieParam

但是我收到以下错误:

@Override
public void cookieTest( @CookieParam("testToken") String testCookie, HttpServletRequest request ) {

    Cookie[] cookies = request.getCookies();
    for( Cookie cookie : cookies ) {
        if( cookie.getName().equals( "testToken" ) ) {
            System.out.println( "found testToken" );
        }
    }
}

控制器确实有一个接口,但我在两个地方都添加了Caused by: java.lang.IllegalStateException: No parameter name specified for argument of type [java.lang.String], and no parameter name information found in class file either. at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.getRequiredParameterName(HandlerMethodInvoker.java:729) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:488) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:348) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171) ... 67 more 注释,名称正确。如果我删除testCookie参数,我可以遍历CookieParam并看到cookie确实存在。我缺少一步吗?

我正在使用Spring 3和Java 6.

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

这是我的一个控制器方法的声明,它使用了一个cookie:

@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public String home(ModelMap model, HttpServletRequest request, 
        @CookieValue(value = "aCookie", defaultValue = "someRandomValue") String aValue)