在 JAX-WS 请求之间使用变量
Use variable between JAX-WS requests
我正在尝试使用 JAX -WS 开发 Web 应用程序。我的问题似乎很简单,但是我不明白如何解决它。我有 class 个变量,我需要在 GET 和 POST 请求中使用这些值。例如,我在 GET 方法中启动 'response',然后我需要在 POST 方法中使用它,但是当我从 js 调用 POST api/conversation 时,我收到一个错误,因为 'response' 仍然为空。如何保存变量的值?这是我的代码
import javax.ws.rs.*;
@ApplicationPath("api")
@Path("conversation")
public class Conversation {
private final String conversationWorkspace = "myworkspace";
private final static String CONVERSATION_ID = "myid";
private final static String CONVERSATION_PASS = "mypass";
private MessageRequest request;
private MessageResponse response;
private ConversationService service;
@GET
@Produces("application/text")
public String getInitiatePhrase(){
service = new ConversationService("2017-05-26", CONVERSATION_ID, CONVERSATION_PASS);
response = service.message(conversationWorkspace, null).execute(); //here response gets its value
return response.getText().get(0);
}
@POST
@Produces("application/text")
@Consumes("application/text")
public String getBotAnswer(String userText){
System.out.println("response " + response);
request = new MessageRequest.Builder().inputText(userText).context(response.getContext()).build(); //response must not be null
response = service.message(conversationWorkspace, request).execute();
return response.getText().get(0);
}
}
有问题的 Java class 似乎不是容器管理的 bean。当您对 GET 和随后的 POST 方法进行休息服务调用时,会创建两个单独的 Conversation class 实例。因此,class 字段响应在第二个 POST 调用中将为空。
有多种方法可以解决这个问题。然而,采取的方法取决于回答这个问题:服务真的应该知道两个独立的客户端请求吗?或者客户端应该进行一次 GET 调用,然后向后续的 POST 提供所需的信息。
我会使用下面提到的方法 1,除非有充分的理由使用 2、3 或 4。(2、3 和 4 相似,只是它们是不同的规范/框架)
- 客户端缓存 GET 的响应,并通过 POST 请求
发回所需信息
- 使用 EE 有状态会话 bean (http://docs.oracle.com/javaee/6/tutorial/doc/gipjg.html)
- 使用 CDI 会话范围的 bean (http://docs.oracle.com/javaee/6/tutorial/doc/gjbbk.html)
- 使用 spring 会话作用域 bean (http://springinpractice.com/2008/05/08/session-scoped-beans-in-spring / https://tuhrig.de/making-a-spring-bean-session-scoped/)
我正在尝试使用 JAX -WS 开发 Web 应用程序。我的问题似乎很简单,但是我不明白如何解决它。我有 class 个变量,我需要在 GET 和 POST 请求中使用这些值。例如,我在 GET 方法中启动 'response',然后我需要在 POST 方法中使用它,但是当我从 js 调用 POST api/conversation 时,我收到一个错误,因为 'response' 仍然为空。如何保存变量的值?这是我的代码
import javax.ws.rs.*;
@ApplicationPath("api")
@Path("conversation")
public class Conversation {
private final String conversationWorkspace = "myworkspace";
private final static String CONVERSATION_ID = "myid";
private final static String CONVERSATION_PASS = "mypass";
private MessageRequest request;
private MessageResponse response;
private ConversationService service;
@GET
@Produces("application/text")
public String getInitiatePhrase(){
service = new ConversationService("2017-05-26", CONVERSATION_ID, CONVERSATION_PASS);
response = service.message(conversationWorkspace, null).execute(); //here response gets its value
return response.getText().get(0);
}
@POST
@Produces("application/text")
@Consumes("application/text")
public String getBotAnswer(String userText){
System.out.println("response " + response);
request = new MessageRequest.Builder().inputText(userText).context(response.getContext()).build(); //response must not be null
response = service.message(conversationWorkspace, request).execute();
return response.getText().get(0);
}
}
有问题的 Java class 似乎不是容器管理的 bean。当您对 GET 和随后的 POST 方法进行休息服务调用时,会创建两个单独的 Conversation class 实例。因此,class 字段响应在第二个 POST 调用中将为空。
有多种方法可以解决这个问题。然而,采取的方法取决于回答这个问题:服务真的应该知道两个独立的客户端请求吗?或者客户端应该进行一次 GET 调用,然后向后续的 POST 提供所需的信息。
我会使用下面提到的方法 1,除非有充分的理由使用 2、3 或 4。(2、3 和 4 相似,只是它们是不同的规范/框架)
- 客户端缓存 GET 的响应,并通过 POST 请求 发回所需信息
- 使用 EE 有状态会话 bean (http://docs.oracle.com/javaee/6/tutorial/doc/gipjg.html)
- 使用 CDI 会话范围的 bean (http://docs.oracle.com/javaee/6/tutorial/doc/gjbbk.html)
- 使用 spring 会话作用域 bean (http://springinpractice.com/2008/05/08/session-scoped-beans-in-spring / https://tuhrig.de/making-a-spring-bean-session-scoped/)