Frage

Is there a way to control the output format from the client side?

I have a class which

@Produces(MediaType.TEXT_HTML)

and I want it to produce json when the client requests.

I can copy the class verbatim, replacing only the @Path and @Produces annotations, but this looks like a total waste.

I wonder if the client could append something like &content-type=application/json to the URL and have my server respond with json instead of html?

War es hilfreich?

Lösung

You'll need to annotate the resources as providing both HTML and JSON:

@Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON})

Then just make sure you have MessageBodyWriter implementations registered to handle the method's return type. Dropwizard's JacksonMessageBodyProvider should handle the JSON; if you're using Dropwizard Views, ViewMessageBodyWriter should handle the rest. Jersey will do the content negotiation with the client, provided your client has application/json in the request's Accept header.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top