Skip to content

Instantly share code, notes, and snippets.

@cedricziel
Last active August 29, 2015 14:24
Show Gist options
  • Save cedricziel/80dd964646d2477c2c62 to your computer and use it in GitHub Desktop.
Save cedricziel/80dd964646d2477c2c62 to your computer and use it in GitHub Desktop.
Configuring Entity wrapping with Spring Data Rest. Ember Data HAL 9000 for example requires this.
package com.cedricziel.tcagen.backend.domain;
@Data
@NoArgsConstructor
@Entity
@Table(name = "projects")
// JsonRootDetermines the enclosuere for the entity in json responses
@JsonRootName("project")
public class Project implements Serializable {
// ..
}
package com.cedricziel.tcagen.backend.config;
import com.cedricziel.tcagen.backend.domain.Extension;
import com.cedricziel.tcagen.backend.domain.Project;
import com.cedricziel.tcagen.backend.domain.TcaTable;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@Configuration
public class SpringDataRestConfig extends RepositoryRestMvcConfiguration {
@Autowired(required = false)
private Jackson2ObjectMapperBuilder objectMapperBuilder;
@Bean
@ConfigurationProperties(prefix = "spring.data.rest")
@Override
public RepositoryRestConfiguration config() {
return super.config();
}
@Override
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
if (this.objectMapperBuilder != null) {
this.objectMapperBuilder.configure(objectMapper);
}
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
}
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
super.configureRepositoryRestConfiguration(config);
config.exposeIdsFor(Project.class);
config.exposeIdsFor(Extension.class);
config.exposeIdsFor(TcaTable.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment