I'm about to develop a JAX-RS based RESTful web service and I use MOXy (JAXB) in order to automatically generate my web service's JSON responses.
Everything is cool, but due to the fact that the web service will be the back-end of a JavaScript-based web application and therefore publicly accessible I don't want to expose certain details like class names, etc.
But, I've realized that under certain conditions MOXy embeds a "@type" entry into the marshalled string and this entry is followed by the class name of the object that has just been marshalled.
In particular, I've realized that MOXy behaves in this way when marshalling instances of extended classes.
Assume the following super class "MyBasicResponse"
@XmlRootElement(name="res")
public class MyBasicResponse {
@XmlElement
private String msg;
public MyBasicResponse() {
// Just for conformity
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
And this specialized (extended) class "MySpecialResponse"
@XmlRootElement(name="res")
public class MySpecialResponse extends MyBasicResponse {
@XmlElement
private String moreInfo;
public MySpecialResponse() {
// Just for conformity
}
public String getMoreInfo() {
return moreInfo;
}
public void setMoreInfo(String moreInfo) {
this.moreInfo = moreInfo;
}
}
So, the MyBasicResponse object's marshalled string is
{"msg":"A Message."}
(That's okay!)
But, the MySpecialResponse object's marshalled string is like
{"@type":"MySpecialResponse","msg":"A Message.","moreInfo":"More Information."}
Is there a way to strip the
"@type":"MySpecialResponse"
out of my response?
You can wrap your object in an instance of
JAXBElement
specifying the subclass being marshalled to get rid of the type key. Below is a full example.Java Model
Same as from the question, but with the following
package-info
class added to specifying the field access to match those classesDemo Code
Demo
Output
We see that when the object was marshalled an
type
key was marshalled (corresponding to thexsi:type
attribute in the XML representation), because as MOXy is concerned it was necessary to distinguish betweenMyBasicResponse
andMySpecialResponse
. When we wrapped the object in an instance ofJAXBElement
and qualified the type MOXy didn't need to add thetype
key.For More Information