[BAEL-11597] Move and update Etags article
* Moved etags article related code * Fixed existing etag Ignored test * Added etag scentsio adding etag support to a single endpoint * Added tests for single endpoint etag scenario * Cleaned now unused code in spring-rest-full module
This commit is contained in:
@@ -7,6 +7,7 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@@ -20,6 +21,9 @@ public class Foo implements Serializable {
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
@@ -49,6 +53,14 @@ public class Foo implements Serializable {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
|
||||
import org.springframework.oxm.xstream.XStreamMarshaller;
|
||||
import org.springframework.web.filter.ShallowEtagHeaderFilter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
@@ -45,5 +46,15 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
//
|
||||
// return xmlConverter;
|
||||
// }
|
||||
|
||||
|
||||
// Etags
|
||||
|
||||
// If we're not using Spring Boot we can make use of
|
||||
// AbstractAnnotationConfigDispatcherServletInitializer#getServletFilters
|
||||
@Bean
|
||||
public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
|
||||
return new ShallowEtagHeaderFilter();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -45,6 +46,18 @@ public class FooController {
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
// Note: the global filter overrides the ETag value we set here. We can still analyze its behaviour in the Integration Test.
|
||||
@GetMapping(value = "/{id}/custom-etag")
|
||||
public ResponseEntity<Foo> findByIdWithCustomEtag(@PathVariable("id") final Long id,
|
||||
final HttpServletResponse response) {
|
||||
final Foo resourceById = RestPreconditions.checkFound(service.findOne(id));
|
||||
|
||||
eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response));
|
||||
return ResponseEntity.ok()
|
||||
.eTag(Long.toString(resourceById.getVersion()))
|
||||
.body(resourceById);
|
||||
}
|
||||
|
||||
// read - one
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<!-- NOTE: web.xml is not used in Spring Boot. This is just for guidance, showing how an Etag Filter would be implemented using XML-based configs -->
|
||||
|
||||
<!-- <?xml version="1.0" encoding="UTF-8"?> -->
|
||||
<!-- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" -->
|
||||
<!-- xsi:schemaLocation=" -->
|
||||
<!-- http://java.sun.com/xml/ns/javaee -->
|
||||
<!-- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0" -->
|
||||
<!-- > -->
|
||||
|
||||
<!-- <filter> -->
|
||||
<!-- <filter-name>etagFilter</filter-name> -->
|
||||
<!-- <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class> -->
|
||||
<!-- </filter> -->
|
||||
<!-- <filter-mapping> -->
|
||||
<!-- <filter-name>etagFilter</filter-name> -->
|
||||
<!-- <url-pattern>/*</url-pattern> -->
|
||||
<!-- </filter-mapping> -->
|
||||
<!-- </web-app> -->
|
||||
Reference in New Issue
Block a user