#124 - Fixed mapping annotations in ResourceSupport types.

Added @XmlAnyElement to Resource and moved annotations to accessors to avoid name clashes when overriding them using Jackson mixins.
This commit is contained in:
Oliver Gierke
2013-12-16 15:12:38 +01:00
parent 27069b82d6
commit fac6bbb2cc
5 changed files with 51 additions and 15 deletions

View File

@@ -34,8 +34,6 @@ public class PagedResources<T> extends Resources<T> {
public static PagedResources<?> NO_PAGE = new PagedResources<Object>();
@org.codehaus.jackson.annotate.JsonProperty("page")//
@com.fasterxml.jackson.annotation.JsonProperty("page")//
private PageMetadata metadata;
/**
@@ -73,6 +71,8 @@ public class PagedResources<T> extends Resources<T> {
*
* @return the metadata
*/
@org.codehaus.jackson.annotate.JsonProperty("page")
@com.fasterxml.jackson.annotation.JsonProperty("page")
public PageMetadata getMetadata() {
return metadata;
}

View File

@@ -17,6 +17,7 @@ package org.springframework.hateoas;
import java.util.Arrays;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.springframework.util.Assert;
@@ -29,8 +30,6 @@ import org.springframework.util.Assert;
@XmlRootElement
public class Resource<T> extends ResourceSupport {
@org.codehaus.jackson.annotate.JsonUnwrapped
@com.fasterxml.jackson.annotation.JsonUnwrapped
private final T content;
/**
@@ -68,6 +67,9 @@ public class Resource<T> extends ResourceSupport {
*
* @return the content
*/
@org.codehaus.jackson.annotate.JsonUnwrapped
@com.fasterxml.jackson.annotation.JsonUnwrapped
@XmlAnyElement
public T getContent() {
return content;
}

View File

@@ -30,9 +30,6 @@ import org.springframework.util.Assert;
*/
public class ResourceSupport implements Identifiable<Link> {
@XmlElement(name = "link", namespace = Link.ATOM_NAMESPACE)
@org.codehaus.jackson.annotate.JsonProperty("links")
@com.fasterxml.jackson.annotation.JsonProperty("links")
private final List<Link> links;
public ResourceSupport() {
@@ -94,6 +91,9 @@ public class ResourceSupport implements Identifiable<Link> {
*
* @return
*/
@XmlElement(name = "link", namespace = Link.ATOM_NAMESPACE)
@org.codehaus.jackson.annotate.JsonProperty("links")
@com.fasterxml.jackson.annotation.JsonProperty("links")
public List<Link> getLinks() {
return Collections.unmodifiableList(links);
}

View File

@@ -35,10 +35,6 @@ import org.springframework.util.Assert;
@XmlRootElement(name = "entities")
public class Resources<T> extends ResourceSupport implements Iterable<T> {
@XmlAnyElement
@XmlElementWrapper
@org.codehaus.jackson.annotate.JsonProperty("content")
@com.fasterxml.jackson.annotation.JsonProperty("content")
private final Collection<T> content;
/**
@@ -100,6 +96,10 @@ public class Resources<T> extends ResourceSupport implements Iterable<T> {
*
* @return the content will never be {@literal null}.
*/
@XmlAnyElement
@XmlElementWrapper
@org.codehaus.jackson.annotate.JsonProperty("content")
@com.fasterxml.jackson.annotation.JsonProperty("content")
public Collection<T> getContent() {
return Collections.unmodifiableCollection(content);
}

View File

@@ -18,6 +18,13 @@ package org.springframework.hateoas;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.junit.Test;
@@ -30,6 +37,7 @@ import org.junit.Test;
public class ResourceIntegrationTest extends AbstractMarshallingIntegrationTest {
static final String REFERENCE = "{\"links\":[{\"rel\":\"self\",\"href\":\"localhost\"}],\"firstname\":\"Dave\",\"lastname\":\"Matthews\"}";
static final String XML_REFERENCE = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><personResource xmlns:ns2=\"http://www.w3.org/2005/Atom\"><ns2:link href=\"/foo\" rel=\"bar\"/><person lastname=\"Matthews\" firstname=\"Dave\"/></personResource>";
@Test
public void inlinesContent() throws Exception {
@@ -44,6 +52,28 @@ public class ResourceIntegrationTest extends AbstractMarshallingIntegrationTest
assertThat(write(resource), is(REFERENCE));
}
/**
* @see #124
*/
@Test
public void marshalsResourceToXml() throws Exception {
Person person = new Person();
person.firstname = "Dave";
person.lastname = "Matthews";
PersonResource resource = new PersonResource(person);
resource.add(new Link("/foo", "bar"));
JAXBContext context = JAXBContext.newInstance(PersonResource.class, Person.class);
StringWriter writer = new StringWriter();
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(resource, writer);
assertThat(writer.toString(), is(XML_REFERENCE));
}
/**
* @see #14
*/
@@ -58,17 +88,21 @@ public class ResourceIntegrationTest extends AbstractMarshallingIntegrationTest
assertThat(result.getContent().lastname, is("Matthews"));
}
@XmlRootElement
static class PersonResource extends Resource<Person> {
public PersonResource() {
public PersonResource(Person person) {
super(person);
}
protected PersonResource() {}
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
@XmlRootElement
static class Person {
String firstname;
String lastname;
@XmlAttribute String firstname;
@XmlAttribute String lastname;
}
}