0

Currently I have registered a Gson Provider which correctly is used whenever my request is consuming or producing json.

The problem is that I have a request that needs the Post data as either a byte[], InputStream, Reader, or String.

The reason I need the "raw" data is that I have some third party code where it expects to do its own deserialization.

No matter which of these four types I specify my Post method to expect, the GsonReader will complain and rightly so.

Expected a string but was BEGIN_OBJECT

Depending on the type there is a different error, but it all boils down to the fact that I don't want this Provider/MessageBodyReader to run.

Also, I don't have control of the Accept and Content-type headers of the Posted data. They will be application/json.

esiegel
  • 1,773
  • 2
  • 19
  • 31

1 Answers1

1

You can "modify" the accept/content-type headers of a request in a filter. So, if there is any way you can recognize that for this request, you don't want to use GSON, you can write a ContanerRequestFilter that modifies the headers.

If using GSON provider depends on a method the request gets matched to, you can implement ResourceFilterFactory that applies (returns) the ContainerRequestFilter (that modifies the content-type header to something other than json) just for the applicable methods (you can even introduce a custom annotation, annotate such methods with it and in the resourcefilterfactory return the containerrequestfilter only if the method passed to it is annotated with that annotation).

Here are the relevant links:

Martin Matula
  • 7,969
  • 1
  • 31
  • 35
  • I like this solution. Before this, I had chosen to simply overwrite the GsonProvider to not deserialize for Reader Types. Your solution is cleaner and probably more semantically correct. – esiegel Jun 08 '12 at 21:28