You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by LeszekM <sp...@poczta.onet.pl> on 2008/10/23 11:45:10 UTC

CXF how to definie Response Type in Request Header ?

Hi
I am new to CXF and I am trying to write a CXF Service using JAX RS
Annotations. The problem is, that in this case the type of  Servers's
response (XML/JSON), is defined in Request Header like:
X-UI-FORMAT=JSON|XML. 
Here is what I have to do:

@GET
@Path("getCustomer")
public Customer getCustomer() {
   if request.header.equals("application/json") return Customer in JSON
format
   else return Customer in XML format 
}

I couldn't find the solution neither in Forum nor in CXF samples. And
ProduceMime annotation doesn't seem to work in this case, as only one is
allowed.

Is there any solution to this problem ?
Thanks, 
Leszek Mysliwiec

-- 
View this message in context: http://www.nabble.com/CXF-how-to-definie-Response-Type-in-Request-Header---tp20127708p20127708.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: JAXRS: CXF how to definie Response Type in Request Header ?

Posted by LeszekM <sp...@poczta.onet.pl>.
Hi, and many thanks. This is the solution I was looking for. 
-- 
View this message in context: http://www.nabble.com/CXF-how-to-definie-Response-Type-in-Request-Header---tp20127708p20128981.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: JAXRS: CXF how to definie Response Type in Request Header ?

Posted by Sergey Beryozkin <se...@progress.com>.
Hi

>
> Hi
> I am new to CXF and I am trying to write a CXF Service using JAX RS
> Annotations. The problem is, that in this case the type of  Servers's
> response (XML/JSON), is defined in Request Header like:
> X-UI-FORMAT=JSON|XML.

So, if I understand it right, the expected type is defined in the custom header ? Like this :

X-UI-FORMAT=application/json
X-UI-FORMAT=application/xml

while HTTP Accept header may contains some arbitrary values, possibly */* ?

If it's the case then yes, one option is to inject @Context HttpHeaders and do if/else coding, the way you did it.
Similar option is to do add a @HeaderParam("X-UI-FORMAT") parameter in the signature.

CXF specific extension, RequestHandler filter will let you do it in a better way. In this RequestHandler you can update the value of 
HTTP Accept header by preprocessing X-UI-FORMAT, eg :

public class XUiFormatHandler implements RequestHandler {

   public Response handleRequest(Message m, ClassResourceInfo resourceClass) {

         m.put(Message.ACCEPT_CONTENT_TYPE, getXUiFormatValue(m));

   }

}

and then register this filter from Spring like any other provider.
Finally you can do

@ProduceMime("application/xml", "application/json")
public Customer getCustomer()

Did I understand your problem correctly ?

Cheers, Sergey


> Here is what I have to do:
>
> @GET
> @Path("getCustomer")
> public Customer getCustomer() {
>   if request.header.equals("application/json") return Customer in JSON
> format
>   else return Customer in XML format
> }
>
> I couldn't find the solution neither in Forum nor in CXF samples. And
> ProduceMime annotation doesn't seem to work in this case, as only one is
> allowed.
>
> Is there any solution to this problem ?
> Thanks,
> Leszek Mysliwiec
>
> -- 
> View this message in context: http://www.nabble.com/CXF-how-to-definie-Response-Type-in-Request-Header---tp20127708p20127708.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>