0

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.

mkez00
  • 982
  • 1
  • 8
  • 17

3 Answers3

1

With Jackson as your Json view processor you can add the following on top of your class:

@JsonIgnoreProperties("pageDescription")

user2144247
  • 19
  • 1
  • 3
  • This worked. Excellent. Going to try Oliver's method of doing the Jackson JAXB integration so i'm not cluttering my model's with annotations. – mkez00 Jun 16 '14 at 11:07
1

If you render JSON, JAXB annotations are going to be considered as JAXB is for binding and rendering XML (as the name of the project and the annotations actually imply).

If you want to customize JSON output, you have two options:

  1. Use Jackson annotations (@Json…). @JsonIgnore on the pageDescription field should do the trick.
  2. Include the Jackson JAXB integration module to your project and configure your Jackson ObjectMapper to include it (as described on the project website).

I recommend the former as it's the simpler approach and you have more control over the JSON rendering.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • The Jackson JAXB integration module worked as expected. Had some config issues which http://stackoverflow.com/questions/9517189/spring-mvc-and-jackson-mapping-do-not-return-the-root-element-in-json helped with. Thanks again. – mkez00 Jun 16 '14 at 12:30
0

Add @XmlTransient to pageDescription will do the trick.

Erik Schmiegelow
  • 2,739
  • 1
  • 18
  • 22