Trying
Trying

Reputation: 14278

@Consumes(MediaType.APPLICATION_JSON) annotation but getting request body as string

I am currently working on a project which was maintained by some other team and now i need to maintain it. While i was going through the project i found some thing below:

In jax-rs controller it was annotated by @Consumes(MediaType.APPLICATION_JSON) but the method takes request body as String rather than JSON. Then what is the use of the annotation? Does it help in content negotiation anyway?

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createCake(final String requestBody){.......}

How it is converting a JSON body to string?

My technology stack if it anyway helps to answer:

JAX-RS Spring 3.2 Jersey 2.4

Upvotes: 0

Views: 5233

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279920

The @Consumes serves the following purpose. It restricts the mapping for your handlers. For example, you may have two handlers for the path /resource, one mapped to consume XML and the other mapped to consume json. The dispatcher will choose the right one based on the request's content-type.

The parameter type can be anything as long as there is an appropriate converter for the specified media type to the parameter type itself. In this case, there's very likely a converter from any media type to String.

Upvotes: 4

Related Questions