i'm using Spring + jersey + mongodb(on mongolab) + maven
the web service part is working fine but when i try to connect to the database i get a null pointer exception on the MongoTemplate i've tried a lot of solutions but none working
here is my code:
applicationContext:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<context:component-scan base-package="com.jersey" />
<!-- Factory bean that creates the Mongo instance -->
<mongo:mongo host="ds031972.mongolab.com" port="31972" id="mongo" />
<mongo:db-factory dbname="proyecto_desarrollo" username="danprat" password="19693058" mongo-ref="mongo" id="mongoDbFactory" />
<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
here the dao that uses the mongoTemplate
public class UserManager {
public static final String COLLECTION_NAME = "user";
@Autowired
private MongoTemplate mongoTemplate;
public List<User> getUsers() {
return mongoTemplate.findAll(User.class , COLLECTION_NAME);
}
}
just in case this is the service file
@Path("/users")
public class UserService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers(){
UserManager userManager = new UserManager();
return userManager.getUsers();
}
}
Connection uri provided by mongolab :
mongodb://<dbuser>:<dbpassword>@ds031972.mongolab.com:31972/proyecto_desarrollo
i believe MongoTemplate is not properly injecting but don't know why im new to spring and mongo
EDIT
this is is the one that worked thanks to the answer given:
@Component
public class UserManager {
public static final String COLLECTION_NAME = "user";
@Autowired
private MongoTemplate mongoTemplate;
public List<User> getUsers() {
return mongoTemplate.findAll(User.class , COLLECTION_NAME);
}
}
@Path("/users")
@Component
public class UserService {
@Autowired
UserManager userManager;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers(){
return userManager.getUsers();
}
}
xml:
<context:component-scan base-package="com.jersey.persistence" />
<context:component-scan base-package="com.jersey.services" />
<context:component-scan base-package="com.jersey.commons.entities" />
<!-- Factory bean that creates the Mongo instance -->
<mongo:mongo host="ds031972.mongolab.com" port="31972" id="mongo" />
<mongo:db-factory dbname="proyecto_desarrollo" username="danprat" password="19693058" mongo-ref="mongo" id="mongoDbFactory" />
<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>