DATAREST-454 - Document conditional operations.

Added documentation of ETag and Last-Modified header handling with Spring Data managed domain types.

Original pull request: #184.
This commit is contained in:
Greg Turnquist
2015-06-24 15:59:06 -05:00
committed by Oliver Gierke
parent 7f1799832d
commit 70f35cd314
7 changed files with 112 additions and 7 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.data.rest.webmvc.support;
import static org.springframework.http.HttpHeaders.*;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
@@ -46,6 +48,6 @@ public class ETagArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public ETag resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
return ETag.from(webRequest.getHeader("If-Match"));
return ETag.from(webRequest.getHeader(IF_MATCH));
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.rest.webmvc.jpa;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.rest.webmvc.util.TestUtils.*;
import static org.springframework.http.HttpHeaders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@@ -37,6 +38,7 @@ import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.webmvc.CommonWebTests;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.RelProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
@@ -588,12 +590,12 @@ public class JpaWebTests extends CommonWebTests {
mvc.perform(
patch(builder.build().toUriString()).content("{ \"saleItem\" : \"SpringyBurritos\" }")
.contentType(MediaType.APPLICATION_JSON).header("If-Match", concurrencyTag)).andExpect(
.contentType(MediaType.APPLICATION_JSON).header(IF_MATCH, concurrencyTag)).andExpect(
status().is2xxSuccessful());
mvc.perform(
patch(builder.build().toUriString()).content("{ \"saleItem\" : \"SpringyTequila\" }")
.contentType(MediaType.APPLICATION_JSON).header("If-Match", "\"falseETag\"")).andExpect(
.contentType(MediaType.APPLICATION_JSON).header(IF_MATCH, "\"falseETag\"")).andExpect(
status().isPreconditionFailed());
}

View File

@@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.http.HttpHeaders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.math.BigDecimal;
@@ -193,12 +194,12 @@ public class MongoWebTests extends CommonWebTests {
mvc.perform(
patch(builder.build().toUriString()).content("{ \"saleItem\" : \"SpringyBurritos\" }")
.contentType(MediaType.APPLICATION_JSON).header("If-Match", concurrencyTag)).andExpect(
.contentType(MediaType.APPLICATION_JSON).header(IF_MATCH, concurrencyTag)).andExpect(
status().is2xxSuccessful());
mvc.perform(
patch(builder.build().toUriString()).content("{ \"saleItem\" : \"SpringyTequila\" }")
.contentType(MediaType.APPLICATION_JSON).header("If-Match", concurrencyTag)).andExpect(
.contentType(MediaType.APPLICATION_JSON).header(IF_MATCH, concurrencyTag)).andExpect(
status().isPreconditionFailed());
}

View File

@@ -27,14 +27,16 @@ import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author Pablo Lozano
*/
// tag::code[]
@Document
public class Receipt {
public @Id String id;
public @Version Long version;
public @LastModifiedDate Date date;
public @LastModifiedDate Date date; // <1>
public String saleItem;
public BigDecimal amount;
}
// end::code[]

View File

@@ -175,14 +175,16 @@ public class ETagUnitTests {
assertThat(headers.containsKey("ETag"), is(false));
}
// tag::versioned-sample[]
public class Sample {
@Version Long version;
@Version Long version; // <1>
Sample(Long version) {
this.version = version;
}
}
// end::versioned-sample[]
public class SampleWithoutVersion {}
}