0
我想知道如何設置自定義響應消息給我的MessageContext?我知道如何在服務器端設置HTTP_RESPONSE_CODE
,如200
,404
等,以及如何在客戶端讀取它們。但是,我將如何設置一個自定義消息,如「錯誤的請求,鍵和值不能爲空」?是否有任何旨在用於創建自定義響應消息的內容?在客戶端使用MessageContext定製響應消息
服務器
@WebServiceProvider
@ServiceMode(value = Service.Mode.PAYLOAD)
public class MWServiceProvider implements Provider<Source> {
@javax.annotation.Resource(type=WebServiceContext.class)
private WebServiceContext wsContext;
@Override
public Source invoke(Source source) {
MessageContext messageContext = wsContext.getMessageContext();
...
// read the source content, which is a key-value-pair for this example
if (keyValuePair.getKey().isEmpty() || keyValuePair.getValue().isEmpty()) {
// bad request, key or value can not be empty
messageContext.put(MessageContext.HTTP_RESPONSE_CODE, HttpURLConnection.HTTP_BAD_REQUEST);
// ??? HOW CAN I SET A CUSTOM MESSAGE LIKE
// ??? "Bad request, key and value can not be empty"
// ??? TO MY MESSAGE RESPONSE CONTEXT?
} else {
...
messageContext.put(MessageContext.HTTP_RESPONSE_CODE, HttpURLConnection.HTTP_OK);
...
}
return source;
}
...
}
讀狀態代碼:
Map<String, Object> response = dispatch.getResponseContext();
Integer statusCode = (Integer) response.get(MessageContext.HTTP_RESPONSE_CODE);
System.out.println("status_code: " + statusCode);