0

I don't want one of my Java class model field to appear in the output if my accept type is .accept(MediaType.APPLICATION_XML) but the same field should appear in .accept(MediaType.APPLICATION_JSON).

Is there a way to achieve this?

I tried @XmlAccessorType(XmlAccessType.NONE) as well as @XmlTransient but both seems to hide from JSON output also.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
Bolimera Hannah
  • 195
  • 1
  • 2
  • 11

1 Answers1

1

Once annotations such as @XmlTransient are recognized by both JSON and XML providers, you'd better have tailored DTOs for each media type you support.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Thanks for your reply. Yes I am using Jersey. I didn't know about tailored DTOs. I will look at it and get back in case I have additional questions. – Bolimera Hannah Jan 23 '17 at 14:25
  • @BolimeraHannah The easiest way to achieve what you want is creating a DTO for each media type you are supporting. That is, one DTO for JSON and one DTO for XML. – cassiomolin Jan 23 '17 at 14:34
  • Thank you. Can you please point me to some examples on how to create DTOs ? – Bolimera Hannah Jan 23 '17 at 14:36
  • @BolimeraHannah Have a look at this [Martin Fowler's article](https://martinfowler.com/eaaCatalog/dataTransferObject.html). To give your DTOs a better name, refer to this [answer](http://stackoverflow.com/a/35341664/1426227). – cassiomolin Jan 23 '17 at 14:49