0
我正在嘗試使用JAX -WS來執行Web應用程序。我的問題似乎很簡單,但我不明白如何解決它。我有我需要在GET和POST請求中使用的類變量。例如,我在GET方法中啓動「響應」,然後我需要在POST方法中使用它,但是當我從js調用POST api/conversation時,我收到一個錯誤,因爲'響應'仍然爲空。我如何保存變量的值?這裏是我的代碼在JAX-WS請求之間使用變量
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);
}
}
我使用了1種方法,它工作。謝謝! – Tanya