1

Consider the following entity classes:

  • Manufacturer: represents a car manufacturer (Ford, Volvo, ...) and has a name.
  • Model: represents a model (Fiesta, S80), has a name, and is manufactured by a single manufacturer.

The manufacturer field in the model is annotated as follows:

@ManyToOne
@XmlIDREF
private Manufacturer manufacturer;

I then have two REST resources defined for getting and putting both manufacturers and types. The problem is with putting types:

@PUT
@Consumes("application/xml")
public void putModel(JAXBElement<Model> model) {
    modelFacade.create(model.getValue());
}

and the XML I try to put:

<model>
    <name>Fiesta</name>
    <manufacturer>1</manufacturer>
</model>

The manufacturer element points to 1, a valid instance of Manufacturer, however, when the Model is persisted, the MANUFACTURER_ID is null. How can I get JAXB to read the manufacturer's ID from the XML as well?

Thanks!

Laurens
  • 2,078
  • 5
  • 29
  • 46

2 Answers2

3

Consider adding a hyperlink to the manufacturer instead of an ID (to make it more RESTful). See this e-mail from the users@jersey mailing list which has an example of that.

Martin Matula
  • 7,969
  • 1
  • 31
  • 35
  • Thanks for the answers! Using both posts as a helping hand I was able to get this properly working, thanks! – Laurens Sep 20 '11 at 16:27
1

The following answer I gave to a similar question may help. It makes use of an XmlAdapter to convert the referenced object to/from an ID:

To leverage this in a JAX-RS environment to create a RESTful service you will need to leverage a MessageBodyReader in order to set an instance of EntityManager on the XmlAdapter passed to the Unmarshaller.

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400