I have a Spring MVC web application which uses Hibernate for database persistence. I want to use my existing models to be able to output XML or JSON from a controller.
I configured my desired db model with JAXB annotations and have only specific parameters I want returned in the output (db model example):
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class BaseModel {
@XmlAttribute
String pageTitle;
@XmlTransient
String pageDescription;
The corresponding controller to output XML would simply be
@RequestMapping(value = "/basemodel/raw/xml/{id}", method = RequestMethod.GET, produces = "application/xml")
public @ResponseBody BaseModel getBaseModel(@PathVariable("id") int id){
return service.findBaseModel(id);
}
This would give me the desired XML output with ONLY the pageTitle being output as an attribute and the pageDescription being ignored.
Now, if i were to tweak my controller slightly to produce JSON
@RequestMapping(value = "/basemodel/raw/json/{id}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody BaseModel getBaseModel2(@PathVariable("id") int id){
return service.findBaseModel(id);
}
The corresponding json output would return both the pageTitle and pageDescription as attributes (which I do not want).
I understand there are libraries available which cater to what I would like to accomplish, I just can't seem to get a decent grasp on which one is ideal and also, find a tutorial to configure my application to use these libraries. Can someone please guide me in the right direction? Thank you.