我正在使用spring boot编写代码,它将csv作为输入并创建mongodb集合并将其插入mongodb。
目前我坚持在基于spring boot的代码中使用mongodb。 我在MongoRepository接口上使用save方法时得到NullPointerException。
可能这个问题是由于application.yml文件中的配置不正确以下是src / main / resources目录中application.yml中mongodb特定的更改。
spring: data: mongodb.host: localhost mongodb.port: 27017 mongodb.uri: mongodb://localhost/test mongo.repositories.enabled: trueApplication.java文件如下:
@Configuration @EnableMongoRepositories @Import(RepositoryRestMvcConfiguration.class) @EnableAutoConfiguration // Sprint Boot Auto Configuration @ComponentScan(basePackages = "com.khoubyari.example") public class Application extends SpringBootServletInitializer { private static final Class<Application> applicationClass = Application.class; private static final Logger log = LoggerFactory.getLogger(applicationClass); public static void main(String[] args) { SpringApplication.run(applicationClass, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(applicationClass); } }如果Application.java实现CommandLineRunner并在重写方法运行中编写mongodb插入特定代码,则代码工作正常。 此代码位于以下URL中: https : //spring.io/guides/gs/accessing-data-mongodb/
但是,我的Application.java扩展了SpringBootServletInitializer类并覆盖了configure(SpringApplicationBuilder应用程序)方法。 请查看上面的Application.java。
我需要在基于spring boot的项目的不同包中的实用程序类中使用相同的db特定代码(就像我在Application.java类中提到的URL相同的代码)。
自定义存储库界面如下:
CustomRepository.java: public interface CustomRepository extends MongoRepository<CsvPojo, String>{ }在实用程序类中,我只想注入CustomRepository并使用save方法在mongodb中保存创建的CsvPojo。 但是在执行行customRepository.save(csvPojo)时我正在调整NullPointerException;
请指教! 如果需要其他信息,请告诉我!
此致,Shobhit
I am writing code with spring boot which will take csv as input and create mongodb collection and insert it to mongodb.
Currently I am sticking in using mongodb in spring boot based code. I am getting NullPointerException while using save method on MongoRepository interface.
Probably this problem is due to incorrect configuration in application.yml file Below is mongodb specific changes in application.yml in src/main/resources directory.
spring: data: mongodb.host: localhost mongodb.port: 27017 mongodb.uri: mongodb://localhost/test mongo.repositories.enabled: trueApplication.java file is below:
@Configuration @EnableMongoRepositories @Import(RepositoryRestMvcConfiguration.class) @EnableAutoConfiguration // Sprint Boot Auto Configuration @ComponentScan(basePackages = "com.khoubyari.example") public class Application extends SpringBootServletInitializer { private static final Class<Application> applicationClass = Application.class; private static final Logger log = LoggerFactory.getLogger(applicationClass); public static void main(String[] args) { SpringApplication.run(applicationClass, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(applicationClass); } }If Application.java implements CommandLineRunner and write mongodb insertion specific code in overrided method run, code is working fine. This code is available in this URL: https://spring.io/guides/gs/accessing-data-mongodb/
but, my Application.java extends SpringBootServletInitializer class and override configure(SpringApplicationBuilder application) method. Please look at above for my Application.java.
I need to use same db specific code (as in same code for which URL I mentioned above in Application.java class) in utility class existing in different package for my spring boot based project.
Custom repository interface is below:
CustomRepository.java: public interface CustomRepository extends MongoRepository<CsvPojo, String>{ }In utility class, I just want to inject CustomRepository and use save method to save created CsvPojo in mongodb. But I am geiing NullPointerException while executing line customRepository.save(csvPojo);
Please suggest! If other information is needed, please tell me!
Regards, Shobhit
最满意答案
在spring rest controller中调用save方法解决了问题,而不是从实用程序类调用。
我只是在控制器中注入CustomRepository接口并使用save方法!
我从https://github.com/khoubyari/spring-boot-rest-example学习“关于服务”部分后得到了解决方案
我需要在春季靴子中探索更多,但无论如何问题在上述努力之后得到解决!
Calling save method in spring rest controller resolved the issue in stead of calling from utility class.
I just inject CustomRepository interface in controller and using save method!
I got the solution after studying "About the Service" section from https://github.com/khoubyari/spring-boot-rest-example
I need to explore more in spring boot but anyways problem is resolved after above posted efforts!
更多推荐
发布评论