Best way to create cxf REST json-p service to be used by GWT client

We implement a REST api that returns json-p using CXF and Spring. The service must work with the GWT client. The GWT client invokes the service using JsonpRequestBuilder. It takes place in two function names, one of which must be completed if successful, and the other in case of failure.

The call URL might look something like this: http://our.server.com/restservice?parameter=value...parameter=value&_jsonp=success_callback&_jsonp_failure=failure_callback

When an exception is thrown by a service (service failure), I would like to return an HTTP response with a status of 200, which contains an exception message. The reason I don't use the standard jsonp hooks (JsonpPreStreamInterceptor and JsonpPostStreamInterceptor) is because I would like to wrap the response body with another function if an exception was thrown. This means that my client (built using GWT) can recognize that an error has occurred.

I wrote a converter to convert an exception to a response that looks like this:

import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; import cfpfelles.WebServiceException; @Provider public class WebServiceExceptionMapper implements ExceptionMapper<WebServiceException> { public Response toResponse(WebServiceException arg0) { return Response.status(Response.Status.OK). type("application/x+javascript"). entity("Exception: " + arg0.getMessage()). build(); } } 

I have an intrusion interceptor that stores the names of successful and unsuccessful callbacks in the exchange parts of the Message object, so that they can be accessed through the output interceptors. It looks like this:

 public void handleMessage(Message message) throws Fault { String callbackValue = getCallbackValue(message); if (!callbackValue.isEmpty()) { if (getAcceptType() != null) { message.put("Accept", getAcceptType()); } message.getExchange().put(CALLBACK_KEY, callbackValue); } // failure callback value String failureCallbackValue = getFailureCallbackValue(message); if (!failureCallbackValue.isEmpty()) { message.getExchange().put(FAILURE_CALLBACK_KEY, failureCallbackValue); } } 

I have a preliminary stream interceptor with the handleMessage method that looks like this:

 public void handleMessage(Message message) throws Fault { HttpServletResponse response = (HttpServletResponse) message .get("HTTP.RESPONSE"); try { response.getOutputStream().write("callback(".getBytes("UTF-8")); } catch (IOException e) { throw new Fault(e); } } 

I have an interceptor after a thread with the handleMessage method, which looks like this:

 public void handleMessage(Message message) throws Fault { HttpServletResponse response = (HttpServletResponse) message .get("HTTP.RESPONSE"); try { response.getOutputStream().write(");".getBytes("UTF-8")); } catch (IOException e) { throw new Fault(e); } } 

In the interceptor in front of the thread, I would like to set up a callback wrapper, depending on whether an exception has been thrown. Unfortunately, I cannot figure out how to do this. I don't seem to read the contents of the message in the interceptor before the stream. Also I can not read the HTTP status. It would be very helpful to help with this. Or how to specifically solve my problem, and how to solve it.

+4
source share
1 answer

Is there any specific reason for the send service 200 even in the event of an exception. I mean, the service can return 500, and the message can be analyzed by the GWT client.

0
source

Source: https://habr.com/ru/post/1394065/


All Articles