Created
September 8, 2014 15:50
-
-
Save Xerosigma/02041fdc0849a651dcbf to your computer and use it in GitHub Desktop.
HATEOAS Resouce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.provider.spring.rest; | |
public class Page { | |
private int size; | |
private int totalElements; | |
private int totalPages; | |
private int number; | |
public Page() {} | |
public Page(int size, int totalElements, int totalPages, int number) { | |
super(); | |
this.size = size; | |
this.totalElements = totalElements; | |
this.totalPages = totalPages; | |
this.number = number; | |
} | |
public int getSize() { | |
return size; | |
} | |
public void setSize(int size) { | |
this.size = size; | |
} | |
public int getTotalElements() { | |
return totalElements; | |
} | |
public void setTotalElements(int totalElements) { | |
this.totalElements = totalElements; | |
} | |
public int getTotalPages() { | |
return totalPages; | |
} | |
public void setTotalPages(int totalPages) { | |
this.totalPages = totalPages; | |
} | |
public int getNumber() { | |
return number; | |
} | |
public void setNumber(int number) { | |
this.number = number; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.provider.spring.rest; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.HashMap; | |
import javax.xml.bind.annotation.XmlAnyElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import org.springframework.hateoas.Link; | |
import org.springframework.hateoas.ResourceSupport; | |
import org.springframework.util.Assert; | |
import com.fasterxml.jackson.annotation.JsonUnwrapped; | |
/** | |
* A simple {@link Resource} wrapping a domain object and adding links to it. | |
* | |
* @author Oliver Gierke | |
*/ | |
@XmlRootElement | |
public class Resource<T> extends ResourceSupport { | |
private final T content; | |
private Page page; | |
/** | |
* Creates an empty {@link Resource}. | |
*/ | |
Resource() { | |
this.content = null; | |
this.page = null; | |
} | |
/** | |
* Creates a new {@link Resource} with the given content and {@link Link}s (optional). | |
* | |
* @param content must not be {@literal null}. | |
* @param links the links to add to the {@link Resource}. | |
*/ | |
public Resource(T content, Link... links) { | |
this(content, Arrays.asList(links)); | |
} | |
/** | |
* Creates a new {@link Resource} with the given content and {@link Link}s. | |
* | |
* @param content must not be {@literal null}. | |
* @param links the links to add to the {@link Resource}. | |
*/ | |
public Resource(T content, Iterable<Link> links) { | |
Assert.notNull(content, "Content must not be null!"); | |
Assert.isTrue(!(content instanceof Collection), "Content must not be a collection! Use Resources instead!"); | |
this.content = content; | |
this.add(links); | |
this.page = null; | |
} | |
// FIXME: | |
public Resource(T content, Page page, Link... links) { | |
this(content, Arrays.asList(links), page); | |
} | |
// FIXME: | |
public Resource(T content, Iterable<Link> links, Page page) { | |
Assert.notNull(content, "Content must not be null!"); | |
Assert.isTrue(!(content instanceof Collection), "Content must not be a collection! Use Resources instead!"); | |
this.content = content; | |
this.add(links); | |
this.page = page; | |
} | |
/** | |
* Returns the underlying entity. | |
* | |
* @return the content | |
*/ | |
@JsonUnwrapped | |
@XmlAnyElement | |
public T getContent() { | |
return content; | |
} | |
public Page getPage() { | |
return page; | |
} | |
public void setPage(Page page) { | |
this.page = page; | |
} | |
/* | |
* (non-Javadoc) | |
* @see org.springframework.hateoas.ResourceSupport#toString() | |
*/ | |
@Override | |
public String toString() { | |
return String.format("Resource { content: %s, %s }", getContent(), super.toString()); | |
} | |
/* | |
* (non-Javadoc) | |
* @see org.springframework.hateoas.ResourceSupport#equals(java.lang.Object) | |
*/ | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) { | |
return true; | |
} | |
if (obj == null || !obj.getClass().equals(getClass())) { | |
return false; | |
} | |
Resource<?> that = (Resource<?>) obj; | |
boolean contentEqual = this.content == null ? that.content == null : this.content.equals(that.content); | |
return contentEqual ? super.equals(obj) : false; | |
} | |
/* | |
* (non-Javadoc) | |
* @see org.springframework.hateoas.ResourceSupport#hashCode() | |
*/ | |
@Override | |
public int hashCode() { | |
int result = super.hashCode(); | |
result += content == null ? 0 : 17 * content.hashCode(); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment