On 15/10/12 13:40, Mickael Marrache wrote:
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?
I'm gettinga bit confused.
What exactly are you using tMap for ?
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?
As I said I will consider making the code generator configurable so that
'Response' is returned instead of 'void'
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.
At the moment, 'void' is generated, hence we have to think about the
workarounds. If using the filter does not suit, then experiment with
injecting CXF MessageContext (into the implementation class field) and
set Message.PROTOCOL_HEADERS (of type Map<String, List<String>>) on this
context - that may help
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?
Indeed, hope using MessageContext can help for now.
However if you have a case where a response may or may not contain the
response body then 'tMap' may be actually the 'best friend'. have
representation linking to a schema element but use tMap to get the
generator to include 'Response' instead of the type generated by JAXB
compiler.
The case which is not supported well is when you know that it is always
an empty response but may be the status and headers may vary, in this
case we will need to add a configuration property for 'Response'
generated instead of 'void'
Cheers, Sergey
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
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com