dev@jersey.java.net

Re: [Jersey] GET method that can produce multipart or XML

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 20 Jan 2010 17:22:03 +0000

On Jan 20, 2010, at 5:09 PM, Adam Retter wrote:

> 2010/1/20 Paul Sandoz <Paul.Sandoz_at_sun.com>:
>> Hi Adam,
>>
>> The problem is that Jersey selects the most acceptable media type
>> from the
>> @Produces given what the client sends in the Accept header.
>>
>> In this case i think you want to override what the client accepts
>> and do
>> something like the following:
>>
>> @GET
>> @Path("{rrn: [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}}/pdfs")
>> @Produces("multipart/mixed")
>> public MultiPart get() {
>> if (/not error/) {
>> return ...
>> } else {
>> Bean b = ...
>> throw new WebApplicationException(Response.status(400).entity(b,
>> "text/xml").build());
>> }
>> }
>>
>> I used a 400 to indicate client error but i am not sure what you
>> mean by
>> failure as it could equally be 500 server error.
>>
>> Note that you can also do:
>>
>> @GET
>> @Path("{rrn: [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}}/pdfs")
>> @Produces("multipart/mixed")
>> public Object get() {
>> if (/not error/) {
>> return ...
>> } else {
>> return Response.status(400).entity(b, "text/xml").build();
>> }
>> }
>
> I was using the following code snippets -
>
> @GET
> @Path("{rrn: [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}}/pdfs")
> @Produces({"multipart/mixed", "text/xml"})
> public Response getLodgementPdfsByRRN(
> @PathParam("rrn") String rrn,
> @DefaultValue("1") @QueryParam("languageCode") int languageCode
> )
>
> return Response.status(Response.Status.FORBIDDEN).entity(b).build();
>

OK.


> I cant seem to find the two argument version of entity() that you
> mention,

Doh! i was looking at the static "ok" methods erroneously substituting
for "entity".


> do I actually need to use type() as well, like this -
>
> return Response.status(400).entity(b).type("text/xml").build();
>

Yes, that is correct.

Paul.