如何在REST Jersey Web应用程序中创建、管理、关联会话

时间:2022-02-24 19:34:12

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.

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

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

你能告诉我可以使用哪些技术/ api吗?需要在REST客户端处理一些事情吗?

1 个解决方案

#1


18  

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:

使用JAX-RS用于RESTful web服务相当简单。这是最基本的。您通常定义一个或多个服务类/接口,这些服务类/接口通过JAX-RS注释定义您的REST操作,如下所示:

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

HTTP会话可以像往常一样通过getSession()和getSession(boolean)从HTTP请求对象访问。其他有用的注解包括@RequestParam、@CookieParam甚至@MatrixParam。

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

如果想了解更多信息,你可以阅读RESTEasy User Guide或Jersey User Guide,因为两者都是很好的资源。

#1


18  

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:

使用JAX-RS用于RESTful web服务相当简单。这是最基本的。您通常定义一个或多个服务类/接口,这些服务类/接口通过JAX-RS注释定义您的REST操作,如下所示:

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

HTTP会话可以像往常一样通过getSession()和getSession(boolean)从HTTP请求对象访问。其他有用的注解包括@RequestParam、@CookieParam甚至@MatrixParam。

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

如果想了解更多信息,你可以阅读RESTEasy User Guide或Jersey User Guide,因为两者都是很好的资源。