How to create, manage, associate a session in REST Jersey Web Application

喜你入骨 提交于 2019-12-18 11:53:38

问题


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.

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


回答1:


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 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.

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



来源:https://stackoverflow.com/questions/22377903/how-to-create-manage-associate-a-session-in-rest-jersey-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!