I was wondering how I could set a custom response message to my MessageContext? I know how to set the HTTP_RESPONSE_CODE
like 200
, 404
etc. on server side and how to read them on client side. But how would I set a custom message like "Bad request, key and value can not be empty"? Is there anything which is intended to be used for creating custom response messages?
Server
@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;
}
...
}
Read status code on client side:
Map<String, Object> response = dispatch.getResponseContext();
Integer statusCode = (Integer) response.get(MessageContext.HTTP_RESPONSE_CODE);
System.out.println("status_code: " + statusCode);
You have access to the underlying javax.servlet.http.HttpServletResponse
via key MessageContext.SERVLET_RESPONSE in your MessageContext
. From there you should be able to set the error code and message .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.