spring data rest 사용시 일괄적용으로 @ID 컬럼을 함께 데이터로 내보내고 싶을 때 나는 아래의 코드를 사용한다.
Append configuration file to spring-configuration.
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.RepositoryRestConfigurer;import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;import javax.persistence.Entity;import javax.persistence.EntityManagerFactory;import javax.persistence.metamodel.ManagedType;import javax.persistence.metamodel.Metamodel;import java.util.ArrayList;import java.util.List;@Configurationpublic class RestConfig extends RepositoryRestConfigurerAdapter {
private List<Class<?>> getAllManagedEntityTypes(EntityManagerFactory entityManagerFactory) {
List<Class<?>> entityClasses = new ArrayList<>();
Metamodel metamodel = entityManagerFactory.getMetamodel();
for (ManagedType<?> managedType : metamodel.getManagedTypes()) {
Class<?> javaType = managedType.getJavaType();
if (javaType.isAnnotationPresent(Entity.class)) {
entityClasses.add(managedType.getJavaType());
}
}
return entityClasses;
}
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer(EntityManagerFactory entityManagerFactory) {
List<Class<?>> entityClasses = getAllManagedEntityTypes(entityManagerFactory);
return new RepositoryRestConfigurerAdapter() {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
for (Class<?> entityClass : entityClasses) {
config.exposeIdsFor(entityClass);
}
}
};
}
}
'개발자의 정보 > Java & framework' 카테고리의 다른 글
자바로 파일 인코딩 확인하기 - Check the file encoding in Java language (1) | 2020.02.01 |
---|---|
[JSP] forward된 페이지에서 request.getRequestURI 사용하기 (0) | 2020.02.01 |
Spring framework Page<T> 와 DataTables.js 연동 (0) | 2020.02.01 |
Spring @Controller 에서 파라미터로 배열 받기 (2) | 2020.02.01 |
[spring-boot-starter-data-jpa] with java1.6 (0) | 2020.02.01 |
Spring + StompWebsocket (0) | 2020.02.01 |
FrontEnd 분리된 SpringMVC 구성시 Controller 구성 (0) | 2020.02.01 |
[JSTL]According to TLD or attribute directive in tag file (0) | 2020.02.01 |
댓글