setting default encoding to utf-8 on OpenLiberty jax-rs for POST requests

99 Views Asked by At

I have a REST application that works fine for GET/PUT/DELETE but NOT for POST requests. GET/DELETE consume @PathParametes, POST consumes APPLICATION_FORM_URLENCODED, and PUT consumes the request body as JSON. My data is all in UTF-8 and everything works fine if I send the "charset=utf-8" along with the request. If I don't send the charset along with Content-Type header, the POST request (and only the POST request) is interpreted as ISO-8859. I would like to change that to default as UTF-8. How?

This are my REST endpoints:

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces("application/json; charset=UTF-8")
@Transactional
public Response addNewFederalState(
    @FormParam("name") String name,
    @FormParam("shortName") String shortName) {
  ...
}

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
@Produces("application/json; charset=UTF-8")
public Response updateFederalState(FederalState fs) {
  ...
}

The PUT request seems to default to UTF-8 but the POST request to ISO-8859 ??

How can I set the default to UTF-8 for POST request also?

I'm using OpenLiberty 23.0.0.11, Microprofile6.0/Jakarta10.0.0/JDK17

This works:

curl -X 'POST' 'http://localhost:9080/geo/rest/fs' \
   -H 'accept: */*' \
   -H 'Content-Type: application/x-www-form-urlencoded;charset=utf-8' \
   -d 'name=Baden Würtemberg&shortName=BW

... but I have to explicitly set charset to get the umlaut decoding right.

1

There are 1 best solutions below

1
On

You can add jvm.options file under the server's root with this line:

-Dclient.encoding.override=UTF-8