Given this class annotated with Jackson:
public class ResponseMsg implements Serializable{
public ResponseMsg(String ret, String msg){
this.returnCode = ret;
this.errorMsg = msg;
}
@JsonProperty("ret")
private int returnCode;
@JsonProperty("msg")
private String errorMessage;
// .... getters and setters
}
I have this class working fine in Wildfly 26, it returns a JSON response with the annotated attributes of ResponseMsg:
@Path("/test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response processMessage() {
@POST
@Path("/proc")
public Response process() {
ResponseMsg er = new ResponseMsg("abc","some message");
return Response.ok(er).build();
}
}
Since it's annotated with the JsonProperty, I'm getting in the browser as expected:
{ "ret": "abc", "msg": "some message" }
Now, I'm migrating to Tomee with Jakarta EE 9.1, and the same code returns the ResponseMsg but without the annotated properties, the JSON looks like this:
{ "returnCode": "abc", "errorMessage": "some message" }
Something is missing in Tomee's configuration, what is it?
According to the Jakarta Documentation,
https://jakarta.ee/specifications/jsonb/2.0/jakarta-jsonb-spec-2.0.html#customizing-property-names
There are two standard ways how to customize serialization of field (or JavaBean property) to JSON document. The same applies to deserialization. The first way is to annotate field (or JavaBean property) with jakarta.json.bind.annotation.JsonbProperty annotation. The second option is to set jakarta.json.bind.config.PropertyNamingStrategy.
You need to use the @JsonBProperty
annotation, instead of JsonProperty that you are using.
import jakarta.json.bind.annotation.JsonbProperty;