【问题标题】:dropwizard 0.7 @Session annotationdropwizard 0.7 @Session 注释
【发布时间】:2014-07-05 19:12:09
【问题描述】:

我正在尝试让 Dropwizard 处理会话。 我在 0.7 中读过,Dropwizard 添加了会话支持。 来自发行说明: “添加了对 HTTP 会话的支持。将带注释的参数添加到您的资源方法中:@Session HttpSession 会话以注入会话上下文。”

我有一个示例资源类,它从 http get 请求中获取用户/密码,我想将用户名保存在会话中:

@GET
public Response auth(@QueryParam("user") String user, @QueryParam("password") String password,         @Session HttpSession session) throws URISyntaxException {
    URI rootUri = getApplicationRootUri();
    if(!user.equals(config.getUser()) || !password.equals(config.getPassword())) {

        return Response.temporaryRedirect(rootUri).build();
    }
    session.setAttribute("user", user);
    return Response.temporaryRedirect( new URI("/../index.html")).build();
}

当我尝试从我的应用登录时:

ERROR [2014-05-17 10:33:45,244] com.sun.jersey.spi.container.ContainerRequest: A message body reader for Java class javax.servlet.http.HttpSession, and Java type interface javax.servlet.http.HttpSession, and MIME media type application/octet-stream was not found.

我尝试从 github 上的 dropwizard 源中查看一些测试,似乎 @Session 注释的使用方式与我的代码中相同。

有什么想法吗?

谢谢!

【问题讨论】:

  • 从异常消息“未找到 MIME 媒体类型应用程序/八位字节流”中获取提示:异常可能是由于未指定和正确处理响应类型造成的吗?而不是因为会话。
  • 我是 http 新手,但我也尝试从 dropwizard src (github.com/dropwizard/dropwizard/tree/master/dropwizard-jersey/…) 复制会话测试,并发送带有纯文本作为内容类型的 http 帖子 (curl -X GET -H "Content-Type:text/plain" localhost:8080/session -d "pipp"),得到了同样的错误。 .删除 HttpSession 参数时,一切正常。
  • 启动jetty服务器时好像没有注册HttpSessionProvider,是否需要其他配置才能启用会话?谢谢

标签: java session dropwizard


【解决方案1】:

我在应用程序类中必须做的(dropwizard 0.7)是:

    environment.jersey().register(HttpSessionProvider.class);
    environment.servlets().setSessionHandler(new SessionHandler());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多