How to get JAX-RS (Jersey) to use a specific Locale when parsing url parameters and JSON? (Ideally inferred from the "Accept-Language" header)

StackOverflow https://stackoverflow.com/questions/19452058

Question

To be more specific, right now I'm having a problem with the separator on decimal numbers (comma vs dot): If the wrong separator comes in a url parameter the webservice returns 404. But I'd like to find a proper solution that will handle the JSON reading/writing as well.

Note: I've tried the answer in this other question but it references classes I don't have in my Jersey jars (If, QuantityXmlAdapter and NumberPersonalizedXmlAdapter) and I can't find a clue about them on Google.

Was it helpful?

Solution

I read your questions as this.

Your method

@GET
@Path('/resource/{decimal}')
public Response getResoureWithDecimal(@PathParam("decimal") double decimal)

Your request

GET /resource/1,2

Clearly, 1,2 is not a Java double and JAX-RS has no way to convert it to one.

Possible solution

Use a String as the parameter type and convert it with your own converter.

@GET
@Path('/resource/{decimal}')
public Response getResoureWithDecimal(@PathParam("decimal") String decimal) {
  double decimalAsDouble = convertStringWithCommaToDouble(decimal);
  // ...
}

The implementation of convertStringWithCommaToDouble(String) is up to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top