[JAVA-22] Moved articles not in ebook from spring-boot-rest to spring-boot-mvc-2

This commit is contained in:
Donato Rimenti
2020-03-18 10:34:00 +01:00
parent 932eba6929
commit 2261dd80d4
21 changed files with 1080 additions and 5 deletions
@@ -0,0 +1,95 @@
package com.baeldung.etag;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String name;
@Version
private long version;
public Foo() {
super();
}
public Foo(final String name) {
super();
this.name = name;
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
//
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Foo other = (Foo) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
}
}
@@ -0,0 +1,58 @@
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);
}
}
@@ -0,0 +1,8 @@
package com.baeldung.etag;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface FooDao extends CrudRepository<Foo, Long>{
}
@@ -0,0 +1,13 @@
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);
}
}
@@ -0,0 +1,28 @@
package com.baeldung.etag;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
@Configuration
public class WebConfig {
// Etags
// If we're not using Spring Boot we can make use of
// AbstractAnnotationConfigDispatcherServletInitializer#getServletFilters
@Bean
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() {
FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>( new ShallowEtagHeaderFilter());
filterRegistrationBean.addUrlPatterns("/foos/*");
filterRegistrationBean.setName("etagFilter");
return filterRegistrationBean;
}
// We can also just declare the filter directly
// @Bean
// public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
// return new ShallowEtagHeaderFilter();
// }
}
@@ -0,0 +1,13 @@
package com.baeldung.students;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootStudentsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootStudentsApplication.class, args);
}
}
@@ -0,0 +1,53 @@
package com.baeldung.students;
public class Student {
private long id;
private String firstName;
private String lastName;
public Student() {}
public Student(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public Student(long id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
@@ -0,0 +1,74 @@
package com.baeldung.students;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import com.baeldung.students.StudentService;
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService service;
@GetMapping("/")
public List<Student> read() {
return service.readAll();
}
@GetMapping("/{id}")
public ResponseEntity<Student> read(@PathVariable("id") Long id) {
Student foundStudent = service.read(id);
if (foundStudent == null) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(foundStudent);
}
}
@PostMapping("/")
public ResponseEntity<Student> create(@RequestBody Student student) throws URISyntaxException {
Student createdStudent = service.create(student);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(createdStudent.getId())
.toUri();
return ResponseEntity.created(uri)
.body(createdStudent);
}
@PutMapping("/{id}")
public ResponseEntity<Student> update(@RequestBody Student student, @PathVariable Long id) {
Student updatedStudent = service.update(id, student);
if (updatedStudent == null) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(updatedStudent);
}
}
@DeleteMapping("/{id}")
public ResponseEntity<Object> deleteStudent(@PathVariable Long id) {
service.delete(id);
return ResponseEntity.noContent().build();
}
}
@@ -0,0 +1,51 @@
package com.baeldung.students;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
// DB repository mock
private Map<Long, Student> repository = Arrays.asList(
new Student[]{
new Student(1, "Alan","Turing"),
new Student(2, "Sebastian","Bach"),
new Student(3, "Pablo","Picasso"),
}).stream()
.collect(Collectors.toConcurrentMap(s -> s.getId(), Function.identity()));
// DB id sequence mock
private AtomicLong sequence = new AtomicLong(3);
public List<Student> readAll() {
return repository.values().stream().collect(Collectors.toList());
}
public Student read(Long id) {
return repository.get(id);
}
public Student create(Student student) {
long key = sequence.incrementAndGet();
student.setId(key);
repository.put(key, student);
return student;
}
public Student update(Long id, Student student) {
student.setId(id);
Student oldStudent = repository.replace(id, student);
return oldStudent == null ? null : student;
}
public void delete(Long id) {
repository.remove(id);
}
}