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

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

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

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

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

kotlin data class + bean validation jsr 303

You need to use Annotation use-site targets since the default for a property declared in the constructor is to target the annotation on the constructor parameter instead of the getter (which will be seen by JavaBeans compliant hosts) when there are multiple options available. Also using a data class might be inappropriate here (see note … Read more

Enable HAL serialization in Spring Boot for custom controller method

There’s a lot of aspects here: I doubt that the collection resource at /transactions really returns an individual transaction as you described. Those representations are returned for item resources. If TransactionRepository already is a PageableAndSortingRepository the collection resource can be tweaked by expanding the URI template exposed in the API root for the link named … Read more

While using Spring Data Rest after migrating an app to Spring Boot, I have observed that entity properties with @Id are no longer marshalled to JSON

By default Spring Data Rest does not spit out IDs. However you can selectively enable it through exposeIdsFor(..) method. You could do this in configuration, something like this @Configuration public static class RepositoryConfig extends RepositoryRestMvcConfiguration { @Override protected void configureRepositoryRestConfiguration( RepositoryRestConfiguration config) { config.exposeIdsFor(Class1.class, Class2.class); } }