Hi,
Arguably the JSON readers/writers could also support text/plain (just
like we do for XML).
It may be possible to utilize a container request filter which
modifies the request to add an appropriate content type based on
contents of the entity:
https://jersey.dev.java.net/nonav/apidocs/1.1.1-ea/jersey/com/sun/jersey/api/container/filter/package-summary.html
Another solution as you indicate is for you to extend from
JSONRootElementProvider. Although this class is in the impl package
which means that it is not part of the supported API, you could extend
it as follows:
@Provider
@Produces({MediaType.APPLICATION_OCTET_STREAM,
MediaType.TEXT_PLAIN})
@Consumes({MediaType.APPLICATION_OCTET_STREAM,
MediaType.TEXT_PLAIN})
public class MyJSONRootElementProvider extends
JSONRootElementProvider {
public MyJSONRootElementProvider(@Context Providers ps) {
super(ps);
}
}
We could move this class and other classes to the API so you can
reliable depend on this.
Paul.
On Aug 14, 2009, at 9:13 AM, Yong Yuan wrote:
> Hi,
>
> Our Jersey-based services need to consume requests from our
> clients . Unfortunately, we have no control over what content type
> our clients may use. Most of the time our clients may send out
> requests in JSON format without specifying any content type, and
> sometimes text/plain may be used. In that case, what's the best way
> of still supporting JAXB-JSON binding. For example, how can I bind
> JSON message to a JavaBean object of type MyDataBean as below?
>
> @POST
> @Consumes({MediaType.APPLICATION_OCTET_STREAM, MediaType.TEXT_PLAIN})
> public Response process(MyDataBean bean){
> ....
> }
>
> I thought about creating my own MessageBodyReader implementation,
> but that will duplicate logics in JSONRootElementProvider. I won't
> be able to extend JSONRootElementProvider either because its
> constructors are package-private. Should I use a
> JSONRootElementProvider as a delegate to my own MessageBodyReader
> implementation? If so, how and where can I grab an instance of
> JSONRootElementProvider? Or any better ways?
>
> Thanks a lot,
>