3

I'm using RESTEasy to send objects over my rest api with JSON. It's very convenient - every field is automatically populated in the JSON object. But there are some fields (e.g. passwordhash) that I'd rather not send. I'd also like to strip out any null values - there's no need to send those.

Can I affect how JSON is created?

I'm using Jettison, RESTEasy 2.0.1.GA and, I think, JAXB, although I'm having an infuriatingly-hard time actually being sure of that.

Riley Lark
  • 20,660
  • 15
  • 80
  • 128

1 Answers1

6

It depends on what are you using to work with JSON. If you are using JAXB and Jettison, you can use @XmlTransient to avoid sending the annotated fields. If you are using Jackson you can use @JsonIgnore. No matter which option are you using NULL values should be omitted by default.

ascandroli
  • 3,309
  • 1
  • 14
  • 15
  • 1
    Thanks for the tip to those annotations. null values are definitely not being omitted, though, and sometimes my objects are like 90% null fields! – Riley Lark Mar 31 '11 at 12:53
  • Hmmm.. it's weird. Which configuration are you using Jettison + JAXB, Jackson + JAXB or just Jackson? Which resteasy version are you using? – ascandroli Apr 01 '11 at 10:30
  • You know... I don't know. I just started using resteasy 2.0.1 and don't know the specifics. I am using the default configuration, I guess. – Riley Lark Apr 01 '11 at 12:19
  • I've figured out, I think, that I'm not using Jackson, and @XmlTransient seems to have no effect (I annotated the relevant getter function). I'm stuck! – Riley Lark Jun 06 '11 at 00:58
  • So I *was* using Jackson, but didn't have every jar I needed, I guess. @JsonIgnore worked a charm. – Riley Lark Jun 06 '11 at 15:08
  • If you want to skip null values mapping see: http://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null – rgrebski Jan 29 '15 at 15:34