Yes, I can generate my method as expected using the tMap argument. But, I don't 
understand what I'm doing (i.e. the purpose of tMap) and why I need to do that.
The expected behavior would be to add a method parameter (else how to access 
the information the client sent?), why do I need to add extra configuration to 
make this work?

Concerning the returned responses:

In the case the HTTP response's body is empty, the operation method returns 
nothing (void). Even if I build an instance of Response, how to return it? 
Concerning the use of filters, I don't understand how it can help since the 
decision about which response to send (i.e. status code, various HTTP 
headers...) is taken inside the body of the processing method. I don't have 
this decision information inside the filter.

In the case my HTTP response's body should include a XML representation, the 
generated method returns an instance of the class corresponding to the XML type 
(e.g. AType according to my previous example). So, there is no Response 
instance returned (at least inside the method), and I get the same issue.

The solution seems to be accessing some sort of context inside the method, and 
customize the returned response through this context, but I don't know if there 
is such a construct?

Thanks,
Mickael


-----Original Message-----
From: Sergey Beryozkin [mailto:[email protected]] 
Sent: Monday, October 15, 2012 11:40 AM
To: [email protected]
Subject: Re: Customize HTTP response for REST service

Hi
On 14/10/12 11:40, Mickael Marrache wrote:
> Hi again
>
> I have a question about returning responses to HTTP requests in the context 
> of a RESTful web service.
>
> This is my method declaration in the WADL:
>
> <method id="saveA" name="PUT">
>     <request>
>        <representation mediaType="application/xml" element="ns1:A" />
>     </request>
>     <response status="200">
>         <param name="header1" style="header" type="xsd:string" />
>     </response>
> </method>
>
> After generating the code, I get the following method signature:
>
> public void saveA(AType atype);
>

Does the method is generated as expected ? (referring to your other email re 
PUT method)

> If the PUT operation performed well, I would like to return an HTTP response 
> with status code 200 and an HTTP header containing a certain value. This is 
> indicated in the WADL, but I don't see anything in the code. Since the method 
> saveA doesn't return anything, where is the right place to customize the 
> response (i.e. setting the desired status code and header)?
>
Typically a Response would be returned from the method in such cases, built 
like this:

Response.status(myStatus).header(key, value).build();

I wonder if the generator should generate 'Response' by default in such cases, 
where no response representation is expected...Or this should be configurable 
at least, I'll look into it.

In meantime, add ResponseHandler filter (on CXF 2.6.x) or 
ContainerResponseFilter (on CXF 2.7.0) and add a required header from there...

> Also, what if for a given request, different responses may be returned 
> according to how the operation  performed? Should I add multiple responses in 
> the method element (in the WADL)?

At the moment the simplest thing to do is to

<response status="200 400 401">

etc

Creating multiple responses is also valid but as far as the code generation is 
concerned, it won't make any difference, given that at the code level the 
relevant exceptions are all runtime ones. However, it can be better for the 
reader of WADL because you can attach specific descriptions to different 
response elements...

Cheers, Sergey

>
> Thanks,
> Mickael
>


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to