3

I'm developing a web API in which I want to add support of all the media types, because of functionality requirements. I've tried to give @consumes("*/(") at class level and method level as well. But it didn't worked for me when I tried to access my api with application/json media type in Content-Type header.

I've looked at the implementation of @consumes annotation in which its default value is */* so I tried to use this annotation as @consumes only so it can add default value, but that didn't worked as well. For the time being i've used this annotation as

@Consumes({"application/*","text/*"})

which allows the client to access my API with all possible media types from either of these media type groups. But still if client sends request with multipart/signed or any other media type it messes up.

5
  • To say that an endpoint can handle any type, is to say that you have a provider configured to handle every kind of type. Jersey can't do this. It has support for common types like JSON, XML, multipart, but even those (aside from default support for XML), they still need to be configured (usually by adding the support library and registering them). Best thing to do it not support any type, i.e. */*, and only use those that you know you have configured support for. Commented Feb 26, 2016 at 9:41
  • Check out chapter 8 and 9 for a better understanding of what providers are, and Jersey support for different media types. Commented Feb 26, 2016 at 9:43
  • Yes I want */* to work for me. But it isn't working. If I dont use @consumes annotation at all, then again it gives me exception. Commented Feb 26, 2016 at 9:52
  • Please re-read my comment, and the link I provided. Just because you use */*, doesn't mean your application can support it. You need provider to handle each individual type you advertise support for in the @Consumes annotation Commented Feb 26, 2016 at 9:54
  • Gotcha! Thank you for your response. Commented Feb 26, 2016 at 10:13

1 Answer 1

4

You should just don't use a @Consume annotation on your resource class or method to consume all media types.

From the spec, chapter 3.5, Declaring Media Type Capabilities:

Application classes can declare the supported request and response media types using the @Consumes and @Produces annotations respectively. These annotations MAY be applied to a resource method, a resource class, or to an entity provider (see Section 4.2.3). Use of these annotations on a resource method overrides any on the resource class or on an entity provider for a method argument or return type. In the absence of either of these annotations, support for any media type (“*/*”) is assumed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.