On 14/12/11 16:51, Brice wrote:
Hi


Thanx for your answers :)

I got some more question on the ResponseHandler.

--
Brice


Le mercredi 14 décembre 2011 à 12:41, Sergey Beryozkin a écrit :

Hi
On 14/12/11 09:49, Brice Dutheil wrote:
Hi,

I would like some advice or help in designing a new REST service.

I'm using CXF to serve REST webservices. I already have a custom
interceptors, however I'm not sure how to deal with the following.


Is it possible to 'map' HeaderParams (or FormParam, QueryParam, PathParam)
to custom Java types, instead of a simple string.



Have a look here please:
http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-DealingwithParameters

JAX-RS offers one option: if a type has one of the factory methods then
it will be used; otherwise the CXF ParameterHandler extension will help

Ok, thx, I missed that page when focusing on interceptors.

And eventually answer
something else if the value of the param is not valid.

For example the HTTP Header "Accept-Language" can have several string
values, I would like to map those values to a List<Locale>  in order to
write methods like the one below :

@GET
@Path("/check/stuff/{id}")
public CustomAnswer checkStuffAndAnswerInAcceptedLanguage(@PathParam("id")
int id, @HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) List<Locale>  locales) {
// business stuff
}



I'd recommend injecting HttpHeaders and use its getAcceptableLanguages
method which will a properly sorted List<Locale>; the above will also
work but it's not guaranteed to be sorted AFAIK and a Locale constructor
accepting a single String will be used which will miss on local values
such as 'us' in 'en-us', etc

Yes indeed, before your reply I was planning to use LocaleUtils from 
commons-lang. The locale was also just an example we will have more custom 
headers maybe. Anyway the page seems to explain this case.


Also if the locales are incorrect then answer with a custom response (in
JSON or XML), I'm not sure if I should use the JAXRS response builder or
rely on a provider (we are using GSon in a provider).


Using custom Response, or out filter (ResponseHandler), ExceptionMapper
and even custom MBW will work

I'm not sure to follow you there, the JAX-RS Response class cannot receive a 
body.
Does the ResponseHandler message parameter is the response payload/body ? On 
which we can feed an JSon content?

This time it is my turn to be confused :-)
Basically what I was saying you can use one of the options I listed in order to produce a custom response: do return Response.status(status).entity(customEntity).build() or get the filter overwrite it or throw an exception and get ExceptionMapper producing the response

Does the ExceptionMapper works if I throw something before the INVOKE phase ?

No, ExceptionMappers are checked only if exceptions got originated from the JAX-RS-managed (JAX-RS filters, invoker, the runtime itself, application code) but you can manage the exceptions at the level of CXF interceptors by registering a custom fault out interceptor

Cheers, Sergey



HTH, Sergey


Thanx in advance for your input.


--
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com





Reply via email to