使用Spring数据jpa可以隐藏模型的id和其他属性。我已经在网上尝试了所有可能的解决方案,但似乎没有奏效。我想显示所有返回的空值。
Expose all IDs when using Spring Data Rest
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.hateoas.config.EnableEntityLinks;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.stereotype.Component;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Set;
@Configuration
@Component
@EnableEntityLinks
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class RepositoryConfig implements RepositoryRestConfigurer {
@PersistenceContext
private EntityManager entityManager;
@Override
public void configureRepositoryRestConfiguration(final RepositoryRestConfiguration config) {
final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
final Set<BeanDefinition> beans = provider.findCandidateComponents("io.christdoes.wealth.tracker.entity");
for (final BeanDefinition bean : beans) {
try {
config.exposeIdsFor(Class.forName(bean.getBeanClassName()));
} catch (final ClassNotFoundException e) {
// Can't throw ClassNotFoundException due to the method signature. Need to cast it
throw new IllegalStateException("Failed to expose `id` field due to", e);
}
}
}
}
当我在restclient上发布时,它返回属性的空值。我检查了数据库表,值在那里,但没有显示。我该怎么办?
这是我在restclient上得到的回复
{
"permission": {
"createdBy": null,
"createdDate": null,
"lastModifiedBy": null,
"lastModifiedDate": null,
"id": null,
"name": "CREATE_tes"
},
"_links": {
"permission": {
"href": "http://localhost:8003/permission"
},
"self": {
"href": "http://localhost:8003/permission/id/{id}",
"templated": true
}
}
}