Why is an excerpt projection not applied automatically for a Spring Data REST item resource?

That works as designed. An excerpt projection is used whenever an instance of the target type (UserModel in your case) is used within in an _embedded clause. Thus the excerpt is some kind of preview used everywhere the resource itself is not rendered but pointed to. This is usually the case from collection resources or … Read more

With Spring Data REST, why is the @Version property becoming an ETag and not included in the representation?

No, there is not. The ETag is the HTTP equivalent to what’s expressed as @Value property in the backend. Spring Data REST turns all backend related properties that have a corresponding mechanism in the HTTP protocol into exactly those: ids become URIs (and shouldn’t be part of the payload either), @LastModifiedDate properties become headers, @Version … Read more

How to Log HttpRequest and HttpResponse in a file?

For logging the request Spring has the AbstractRequestLoggingFilter class (well actually one of the subclasses). This can be used to log the incoming request (before and after processing). Depending on the configuration this can include the payload, client information and full URL (including erquest parameters). All these three are disabled by default but can be … Read more

Expose all IDs when using Spring Data Rest

If you want to expose the id field for all your entity classes: import java.util.stream.Collectors; import javax.persistence.EntityManager; 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.RepositoryRestConfigurerAdapter; @Configuration public class MyRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter { @Autowired private EntityManager entityManager; @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.exposeIdsFor(entityManager.getMetamodel().getEntities().stream().map(e -> e.getJavaType()).collect(Collectors.toList()).toArray(new Class[0])); } }

JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value

You need jackson dependency for this serialization and deserialization. Add this dependency: Gradle: compile(“com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.4”) Maven: <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency> After that, You need to tell Jackson ObjectMapper to use JavaTimeModule. To do that, Autowire ObjectMapper in the main class and register JavaTimeModule to it. import javax.annotation.PostConstruct; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @SpringBootApplication public class MockEmployeeApplication { … Read more

Why does RestTemplate not bind response representation to PagedResources?

As you’ve discovered correctly, PagedResources does not have an _embedded property, that’s why you don’t get the content property populated. This dilemma can be solved in two different ways: Providing a type that matches the representation in the first place. Thus, craft a custom class and either stick to the property names of the representation … Read more

Can I make a custom controller mirror the formatting of Spring-Data-Rest / Spring-Hateoas generated classes?

I’ve found a way to imitate the behavior of Spring Data Rest completely. The trick lies in using a combination of the PagedResourcesAssembler and an argument-injected instance of PersistentEntityResourceAssembler. Simply define your controller as follows… @RepositoryRestController @RequestMapping(“…”) public class ThingController { @Autowired private PagedResourcesAssembler pagedResourcesAssembler; @SuppressWarnings(“unchecked”) // optional – ignores warning on return statement below… … Read more

How to consume Page response using Spring RestTemplate

new TypeReference<Page<StoryResponse>>() {} The problem with this statement is that Jackson cannot instantiate an abstract type. You should give Jackson the information on how to instantiate Page with a concrete type. But its concrete type, PageImpl, has no default constructor or any @JsonCreators for that matter, so you can not use the following code either: … Read more

How to work with DTO in Spring Data REST projects?

An approach of how to work with DTO in Spring Data REST projects The working example is here Entities Entities must implement the Identifiable interface. For example: @Entity public class Category implements Identifiable<Integer> { @Id @GeneratedValue private final Integer id; private final String name; @OneToMany private final Set<Product> products = new HashSet<>(); // skipped } … Read more