如何在REST Jersey Web Application中创建,管理和关联会话 [英] How to create, manage, associate a session in REST Jersey Web Application

查看:121
本文介绍了如何在REST Jersey Web Application中创建,管理和关联会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML5 UI连接到后端(REST Jersey到业务逻辑到Hibernate和DB)。
我需要为每个用户登录创建和维护一个会话,直到用户退出。

A HTML5 UI is connected to the backend (REST Jersey to business logic to Hibernate and DB). I need to create and maintain a session for each user login until the user logs out.

您能否指导我使用哪些技术/ API? 。
还需要在REST客户端处理某些事情..

Can you please guide me on what technologies/ APIs can be used. Does something need to be handled at the REST Client end also..

推荐答案

使用JAX-RS进行RESTful Web服务相当简单。以下是基础知识。您通常通过 JAX-RS注释,如下所示:

Using JAX-RS for RESTful web services is fairly straightforward. Here are the basics. You usually define one or more service classes/interfaces that define your REST operations via JAX-RS annotations, like this one:

@Path("/user")
public class UserService {
    // ...
}

您可以通过这些注释在您的方法中自动注入对象:

You can have your objects automagically injected in your methods via these annotations:

// Note: you could even inject this as a method parameter
@Context private HttpServletRequest request;

@POST
@Path("/authenticate")
public String authenticate(@FormParam("username") String username, 
        @FormParam("password") String password) {

    // Implementation of your authentication logic
    if (authenticate(username, password)) {
        request.getSession(true);
        // Set the session attributes as you wish
    }
}

HTTP会话可从 HTTP Request 对象通过 getSession() getSession(boolean)照常。其他有用的注释是 @RequestParam @CookieParam 甚至 @MatrixParam 其他许多人。

HTTP Sessions are accessible from the HTTP Request object via getSession() and getSession(boolean) as usual. Other useful annotations are @RequestParam, @CookieParam or even @MatrixParam among many others.

有关详细信息,您可以阅读 RESTEasy用户指南泽西用户指南,因为两者都是很好的资源。

For further info you may want to read the RESTEasy User Guide or the Jersey User Guide since both are excellent resources.

这篇关于如何在REST Jersey Web Application中创建,管理和关联会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆