users@jersey.java.net

[Jersey] How to configure to remove the prefix _at_ in handling attribute process

From: <feuyeux_at_gmail.com>
Date: Sun, 19 Aug 2012 13:32:32 +0000 (GMT)

I just encountered an issue in my working, which is jersey(JSR311)
always puts prefix @ to attribute from java to json

Analysis:
In com.sun.jersey.api.json. JSONConfiguration, there is a parameter
named usePrefixAtNaturalAttributes, when this parameter is true, the
attribute will be set a prefix @.
Also, I found from
http://jersey.java.net/nonav/documentation/latest/json.html

Solution:
1. Create a class to implement javax.ws.rs.ext.ContextResolver<
JAXBContext> and let jersey to know which class and which attribute
will be treated as element.
private Class[] types = {AbcReturnDto.class };
public AbcAttributeResolver() throws Exception {
        JSONConfiguration jsonConfig =
JSONConfiguration.mapped().attributeAsElement("id", "name",
"value").build();
        this.context = new JSONJAXBContext(jsonConfig, types);
}
2. Add this class to the spring bean management file
<bean class="creative.air.jersey.api.AbcAttributeResolver"
scope="singleton"/>

Limitation:
If there are many attributes need to remove the prefix, it’s not a good
style.

Ideal solution:
Globally, configure the jersey not to add this prefix in a
configuration file or in a line programming.

What do you think my understanding? And it's better to give any advice.
Thanks,
Lu