0

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>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Please post the whole stack trace of your NullPointerException. – TimoStaudinger May 17 '15 at 03:56
  • Is that your real password? – chrylis -cautiouslyoptimistic- May 21 '15 at 18:14
  • Its just a test db that has already been deleted –  May 22 '15 at 01:37
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders May 22 '15 at 21:22
  • @John Sauders Nice to know that info. Sorry for that mistake english is not my main language. –  May 23 '15 at 00:22
  • @DanielPrato: it's not an "English" issue. It's a "Stack Exchange" issue. The site is not about discussion: not even _polite_ discussion. – John Saunders May 23 '15 at 00:23

1 Answers1

0

I don't think you have your application context set up to properly scan classes like UserManager. UserManager doesn't seem to be annotated with the requisite @Component annotation.

Plus you are probably not scanning your own packages to do the injection. I see

<context:component-scan base-package="com.jersey" />

You need to add the package where UserManager lives, and anything else you need scanned. You can do this by appending additional bases classes separated by commas.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • Thank you very much for the answer this was the problem didn't know that annotation was needed and also the base package i thought it was enough to put the basic. –  May 17 '15 at 04:19