I am new to web app development. I am trying to expose the category ID:s so that I can implement "search by category" in my app.
But even after writing the below code I am not able to expose the ID:s.
I have seen some solutions, but they were of very little use to me because of the incompetence.
I am using Eclipse IDE.
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.EntityType;
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.http.HttpMethod;
import com.kausar.ecommerce.entity.Product;
import com.kausar.ecommerce.entity.ProductCategory;
@Configuration
public class MyDataRestConfig implements RepositoryRestConfigurer {
private EntityManager entityManager;
@Autowired
public MyDataRestConfig(EntityManager theEntityManager) {
entityManager = theEntityManager;
}
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
HttpMethod[] theUnsupportedActions = {HttpMethod.PUT, HttpMethod.POST, HttpMethod.DELETE};
// disable HTTP methods for Product: PUT, POST and DELETE
config.getExposureConfiguration()
.forDomainType(Product.class)
.withItemExposure((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions))
.withCollectionExposure((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions));
// disable HTTP methods for ProductCategory: PUT, POST and DELETE
config.getExposureConfiguration()
.forDomainType(ProductCategory.class)
.withItemExposure((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions))
.withCollectionExposure((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions));
// call an internal helper method
exposeIds(config);
}
private void exposeIds(RepositoryRestConfiguration config) {
// expose entity ids
//get a list of all entity classes from the entity manager
Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities();
//create an array of the entity types
List<Class> entityClasses = new ArrayList<>();
//get the entity types for the entities
for (EntityType tempEntityType : entities) {
entityClasses.add(tempEntityType.getJavaType());
}
//expose entity ids for the array of entity/domain types
Class[] domainTypes = entityClasses.toArray(new Class[0]);
config.exposeIdsFor(domainTypes);
}
}