JAVA-8363: Move etag code to spring-boot-mvc-3

This commit is contained in:
Krzysiek
2021-11-25 13:10:58 +01:00
parent 208b9baf5c
commit 8d8ae5daa9
13 changed files with 145 additions and 22 deletions
@@ -1,58 +0,0 @@
package com.baeldung.etag;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
@RestController
@RequestMapping(value = "/foos")
public class FooController {
@Autowired
private FooDao fooDao;
@GetMapping(value = "/{id}")
public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) {
return fooDao.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
// 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 foo = fooDao.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
return ResponseEntity.ok().eTag(Long.toString(foo.getVersion())).body(foo);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Foo create(@RequestBody final Foo resource, final HttpServletResponse response) {
return fooDao.save(resource);
}
@PutMapping(value = "/{id}")
@ResponseStatus(HttpStatus.OK)
public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) {
fooDao.save(resource);
}
@DeleteMapping(value = "/{id}")
@ResponseStatus(HttpStatus.OK)
public void delete(@PathVariable("id") final Long id) {
fooDao.deleteById(id);
}
}
@@ -1,8 +0,0 @@
package com.baeldung.etag;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface FooDao extends CrudRepository<Foo, Long>{
}
@@ -1,13 +0,0 @@
package com.baeldung.etag;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootEtagApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootEtagApplication.class, args);
}
}
@@ -1,6 +1,4 @@
package com.baeldung.etag;
import java.io.Serializable;
package com.baeldung.mime;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -8,6 +6,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import java.io.Serializable;
@Entity
public class Foo implements Serializable {
@@ -18,7 +17,7 @@ public class Foo implements Serializable {
@Column(nullable = false)
private String name;
@Version
private long version;
@@ -57,7 +56,7 @@ public class Foo implements Serializable {
public void setVersion(long version) {
this.version = version;
}
//
@Override
@@ -91,5 +90,4 @@ public class Foo implements Serializable {
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
}
}
@@ -1,4 +1,4 @@
package com.baeldung.etag;
package com.baeldung.mime;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;