在java中使用olingo实现odatav4时,我得到了一个 NullPointerException
.
下面是一个详细的描述--
尝试在我的spring引导应用程序中使用olingo在java中实现odatav4。我在看官方文件https://olingo.apache.org/doc/odata4/tutorials/readep/tutorial_readep.html
我使用的是提供数据的数据库,而不是手动/静态地输入数据。
根据文档,我创建了一个类 Storage.java
模拟数据层。
public class Storage {
@Autowired
CompanyService cservice;
private List<Entity> companyentityList;
public Storage() throws Exception {
companyentityList = new ArrayList<Entity>();
initSampleData();
}
// PUBLIC FACADE
public EntityCollection readEntitySetData(EdmEntitySet edmEntitySet) throws NullPointerException {
// actually, this is only required if we have more than one Entity Sets
if (edmEntitySet.getName().equals(DemoEdmProvider.ES_COMPANY_RECORDS)) {
return getCompaniesData();
}
return null;
}
public Entity readEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams) throws Exception {
EdmEntityType edmEntityType = edmEntitySet.getEntityType();
// actually, this is only required if we have more than one Entity Type
if (edmEntityType.getName().equals(DemoEdmProvider.ET_COMPANY)) {
return getCompany(edmEntityType, keyParams);
}
return null;
}
// INTERNAL
public EntityCollection getCompaniesData() throws NullPointerException {
EntityCollection retEntitySet = new EntityCollection();
for (Entity companyEntity : this.companyentityList) {
retEntitySet.getEntities().add(companyEntity);
}
return retEntitySet;
}
public Entity getCompany(EdmEntityType edmEntityType, List<UriParameter> keyParams) throws Exception {
// the list of entities at runtime
EntityCollection entitySet = getCompaniesData();
// generic approach to find the requested entity
Entity requestedEntity = Util.findEntity(edmEntityType, entitySet, keyParams);
if (requestedEntity == null) {
// this variable is null if our data doesn't contain an entity for the requested
// key
// Throw suitable exception
throw new ODataApplicationException("Entity for requested key doesn't exist",
HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
}
return requestedEntity;
}
// Helper
public void initSampleData() {
try {
getData();
} catch (NullPointerException e) {
System.out.print("<<<<<<<---------------- Database unable to provide data ------------>>>>>>");
}
}
public List<Company> getAllcompanyList() {
Collection<Company> checkingdata = new ArrayList<>();
try {
checkingdata = cservice.getDetails();
} catch (NullPointerException e) {
System.out.print("<<<<<<<---------------- Database unable to provide data ------------>>>>>>");
}
return (List<Company>) checkingdata;
}
// final EntityCollection entitySet = new EntityCollection();
// loop over List<Company> converting each instance of Company into and Olingo Entity
public EntityCollection makeEntityCollection(List<Company> companyList) {
EntityCollection entitySet = new EntityCollection();
for (Company cmp : companyList) {
entitySet.getEntities().add(createEntity(cmp));
}
return entitySet;
}
// Convert instance of cmp object into an Olingo Entity
public Entity createEntity(Company cmp) {
final Entity tmpEntity = new Entity().addProperty(new Property(null, "ID", ValueType.PRIMITIVE, cmp.getId()))
.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, cmp.getContent()));
companyentityList.add(tmpEntity);
return tmpEntity;
}
public void getData() throws NullPointerException {
// ... code to get Data from the DataBase in List and calling makeEntityCollection
List<Company> companyList = getAllcompanyList();
makeEntityCollection(companyList);
// System.out.println(companyList.size());
}
}
在上述代码i中 @Autowired
这个 CompanyService
接口参考 cservice
.
以下是 CompanyService
-
public interface CompanyService {
Collection<Company> getDetails() throws Exception;
} CompanyService
接口由实现 CompanyServiceImplementation
-
@Service
public class CompanyServiceImplementation implements CompanyService{
@Autowired
private CompanyDAOImplementation cDAOWrapper;
public Collection<Company> getDetails() throws Exception {
return cDAOWrapper.findAll();
}
}
在上一节课中 findAll()
方法正在从数据库返回数据。
所以问题是 CompanyService
参考 cservice
哪个是 @Autowired
在 Storage.java
班级是 null
它没有得到初始化,因此我得到一个 NullPointerException
打电话的时候 cservice.getDetails()
.
请让我知道我的代码有什么问题。提前谢谢。
暂无答案!
目前还没有任何答案,快来回答吧!