Flopn Flopn - 1 year ago 122
JSON Question

Jersey MOXy not parsing snake_case

I'm passing a JSON object from a PUT request to my server. The request itself works, however the fields in the JSON which have an underscore (snake_case) seem to bi ignored. The request outputs the received data to see what comes out, and the value with the underscore converts to camelCase, and doesn't get parsed. Here's the class:

Public User{

private int id;
private String name;
private int some_value;


The JSON object I pass to the PUT request:

{ "id":1, "name":John, "some_value":5 }

The PUT method only returns what MOXy caught in this case

public User addUser(User user){
return user;

And the output is:

{ "id":1, "name":John, "someValue":0 }

Notice how "some_value" changed to "someValue" and didn't get the actual value updated. Any idea on why this is happening?

Answer Source

MOXy follows Java Bean conventions by default, which suggest camel case. If you don't want to (or can't) use camel case, you can add an annotation to the field:

@XmlElement(name = "some_value")
private int some_value;

If you don't want to annotate all your fields, use an XMLNameTransformer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download