BAEL-2729 code samples for the article (#12443)
* BAEL-2729 code samples for the article * BAEL-2729 fix formatting issues. Co-authored-by: Yavuz Tas <ytas@vwd.com>
This commit is contained in:
parent
f56d707bf4
commit
2a30030555
@ -0,0 +1,31 @@
|
||||
package com.baeldung.serializeentityid;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
/**
|
||||
* This doesn't work without a projection
|
||||
*/
|
||||
//@JsonProperty
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.baeldung.serializeentityid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.rest.webmvc.RepositoryRestController;
|
||||
import org.springframework.data.web.PagedResourcesAssembler;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.hateoas.PagedModel;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@RepositoryRestController
|
||||
public class PersonController {
|
||||
|
||||
@Autowired
|
||||
PersonRepository repository;
|
||||
|
||||
@GetMapping("/persons")
|
||||
ResponseEntity<?> persons(PagedResourcesAssembler resourcesAssembler) {
|
||||
Page<Person> persons = this.repository.findAll(Pageable.ofSize(20));
|
||||
Page<PersonDto> personDtos = persons.map(PersonDto::new);
|
||||
PagedModel<EntityModel<PersonDto>> pagedModel = resourcesAssembler.toModel(personDtos);
|
||||
return ResponseEntity.ok(pagedModel);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.serializeentityid;
|
||||
|
||||
public class PersonDto {
|
||||
|
||||
private final Long id;
|
||||
|
||||
private final String name;
|
||||
|
||||
public PersonDto(Person person) {
|
||||
this.id = person.getId();
|
||||
this.name = person.getName();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.serializeentityid;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface PersonRepository extends JpaRepository<Person, Long> {
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.serializeentityid;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
import org.springframework.data.rest.core.config.Projection;
|
||||
|
||||
@JsonPropertyOrder({ "id", "name" })
|
||||
@Projection(name = "person-view", types = Person.class)
|
||||
public interface PersonView {
|
||||
|
||||
Long getId();
|
||||
|
||||
String getName();
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.baeldung.serializeentityid;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.metamodel.Type;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
|
||||
@Configuration
|
||||
public class RestConfiguration implements RepositoryRestConfigurer {
|
||||
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
|
||||
final Class<?>[] classes = this.entityManager.getMetamodel()
|
||||
.getEntities()
|
||||
.stream()
|
||||
.map(Type::getJavaType)
|
||||
.toArray(Class[]::new);
|
||||
config.exposeIdsFor(classes);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.serializeentityid;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SerializeEntityIdApp {
|
||||
|
||||
private final PersonRepository repository;
|
||||
|
||||
public SerializeEntityIdApp(PersonRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void onStart() {
|
||||
final Person person1 = new Person();
|
||||
person1.setName("John Doe");
|
||||
final Person person2 = new Person();
|
||||
person2.setName("Markus Boe");
|
||||
|
||||
this.repository.save(person1);
|
||||
this.repository.save(person2);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SerializeEntityIdApp.class, args);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user