olingo odatav4在java中使用@autowired时获得nullpointerexception

zy1mlcev  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(344)

在java中使用olingo实现odatav4时,我得到了一个 NullPointerException .
下面是一个详细的描述--
尝试在我的spring引导应用程序中使用olingo在java中实现odatav4。我在看官方文件https://olingo.apache.org/doc/odata4/tutorials/readep/tutorial_readep.html
我使用的是提供数据的数据库,而不是手动/静态地输入数据。
根据文档,我创建了一个类 Storage.java 模拟数据层。

  1. public class Storage {
  2. @Autowired
  3. CompanyService cservice;
  4. private List<Entity> companyentityList;
  5. public Storage() throws Exception {
  6. companyentityList = new ArrayList<Entity>();
  7. initSampleData();
  8. }
  9. // PUBLIC FACADE
  10. public EntityCollection readEntitySetData(EdmEntitySet edmEntitySet) throws NullPointerException {
  11. // actually, this is only required if we have more than one Entity Sets
  12. if (edmEntitySet.getName().equals(DemoEdmProvider.ES_COMPANY_RECORDS)) {
  13. return getCompaniesData();
  14. }
  15. return null;
  16. }
  17. public Entity readEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams) throws Exception {
  18. EdmEntityType edmEntityType = edmEntitySet.getEntityType();
  19. // actually, this is only required if we have more than one Entity Type
  20. if (edmEntityType.getName().equals(DemoEdmProvider.ET_COMPANY)) {
  21. return getCompany(edmEntityType, keyParams);
  22. }
  23. return null;
  24. }
  25. // INTERNAL
  26. public EntityCollection getCompaniesData() throws NullPointerException {
  27. EntityCollection retEntitySet = new EntityCollection();
  28. for (Entity companyEntity : this.companyentityList) {
  29. retEntitySet.getEntities().add(companyEntity);
  30. }
  31. return retEntitySet;
  32. }
  33. public Entity getCompany(EdmEntityType edmEntityType, List<UriParameter> keyParams) throws Exception {
  34. // the list of entities at runtime
  35. EntityCollection entitySet = getCompaniesData();
  36. // generic approach to find the requested entity
  37. Entity requestedEntity = Util.findEntity(edmEntityType, entitySet, keyParams);
  38. if (requestedEntity == null) {
  39. // this variable is null if our data doesn't contain an entity for the requested
  40. // key
  41. // Throw suitable exception
  42. throw new ODataApplicationException("Entity for requested key doesn't exist",
  43. HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
  44. }
  45. return requestedEntity;
  46. }
  47. // Helper
  48. public void initSampleData() {
  49. try {
  50. getData();
  51. } catch (NullPointerException e) {
  52. System.out.print("<<<<<<<---------------- Database unable to provide data ------------>>>>>>");
  53. }
  54. }
  55. public List<Company> getAllcompanyList() {
  56. Collection<Company> checkingdata = new ArrayList<>();
  57. try {
  58. checkingdata = cservice.getDetails();
  59. } catch (NullPointerException e) {
  60. System.out.print("<<<<<<<---------------- Database unable to provide data ------------>>>>>>");
  61. }
  62. return (List<Company>) checkingdata;
  63. }
  64. // final EntityCollection entitySet = new EntityCollection();
  65. // loop over List<Company> converting each instance of Company into and Olingo Entity
  66. public EntityCollection makeEntityCollection(List<Company> companyList) {
  67. EntityCollection entitySet = new EntityCollection();
  68. for (Company cmp : companyList) {
  69. entitySet.getEntities().add(createEntity(cmp));
  70. }
  71. return entitySet;
  72. }
  73. // Convert instance of cmp object into an Olingo Entity
  74. public Entity createEntity(Company cmp) {
  75. final Entity tmpEntity = new Entity().addProperty(new Property(null, "ID", ValueType.PRIMITIVE, cmp.getId()))
  76. .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, cmp.getContent()));
  77. companyentityList.add(tmpEntity);
  78. return tmpEntity;
  79. }
  80. public void getData() throws NullPointerException {
  81. // ... code to get Data from the DataBase in List and calling makeEntityCollection
  82. List<Company> companyList = getAllcompanyList();
  83. makeEntityCollection(companyList);
  84. // System.out.println(companyList.size());
  85. }

}
在上述代码i中 @Autowired 这个 CompanyService 接口参考 cservice .
以下是 CompanyService -

  1. public interface CompanyService {
  2. Collection<Company> getDetails() throws Exception;

} CompanyService 接口由实现 CompanyServiceImplementation -

  1. @Service
  2. public class CompanyServiceImplementation implements CompanyService{
  3. @Autowired
  4. private CompanyDAOImplementation cDAOWrapper;
  5. public Collection<Company> getDetails() throws Exception {
  6. return cDAOWrapper.findAll();
  7. }

}
在上一节课中 findAll() 方法正在从数据库返回数据。
所以问题是 CompanyService 参考 cservice 哪个是 @AutowiredStorage.java 班级是 null 它没有得到初始化,因此我得到一个 NullPointerException 打电话的时候 cservice.getDetails() .
请让我知道我的代码有什么问题。提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题