HTML5 UI 连接到后端(REST Jersey 到业务逻辑到 Hibernate 和数据库)。 我需要为每个用户登录创建并维护一个 session ,直到用户注销。
能否请您指导我可以使用哪些技术/API。 是否还需要在 REST 客户端处理一些事情..
最佳答案
将 JAX-RS 用于 RESTful Web 服务非常简单。这是基础知识。您通常定义一个或多个服务类/接口(interface),通过 JAX-RS annotations 定义您的 REST 操作,像这样:
@Path("/user")
public class UserService {
// ...
}
您可以通过这些注解将您的对象自动注入(inject)到您的方法中:
// 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 Sessions可从 HTTP Request 访问像往常一样通过 getSession()
和 getSession(boolean)
获取对象。其他有用的注释包括 @RequestParam
、@CookieParam
甚至 @MatrixParam
等等。
有关更多信息,您可能需要阅读 RESTEasy User Guide或 Jersey User Guide因为两者都是极好的资源。
关于java - 如何在 REST Jersey Web 应用程序中创建、管理、关联 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22377903/