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
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
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
HTH, Sergey
Thanx in advance for your input.
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com