Spring MongoRepository is Null -


i have following code attempts save pojo object (actor) mongodb using spring mongo repository, repository object null. have followed multiple examples this one

the pojo class:

@document(collection = "actors") public class actor  {   @id   private string id;   ...   //constructor   //setters & getters } 

the repository:

public interface actorrepository extends mongorepository<actor, string>  {   public actor findbyfnameandlname(string fname, string lname);   public actor findbyfname (string fname);   public actor findbylname(string lname); } 

the service uses repository:

@service public class actorservice  {   @autowired   private actorrepository actorrepository;    public actor insert(actor a)   {     a.setid(null);     return actorrepository.save(a);   } }  

and access service rest controller class:

@restcontroller public class controllers  {    private static final logger logger = logger.getlogger(controllers.class);   private static final applicationcontext ctx = new  annotationconfigapplicationcontext(springmongoconfig.class);    private actorservice actorservice = new actorservice();    @requestmapping(value="/createactor", method=requestmethod.post)   public @responsebody string createactor(@requestparam(value = "fname") string fname,         @requestparam(value = "lname") string lname,         @requestparam(value = "role") string role)   {     return actorservice.insert(new actor(null,fname,lname,role)).tostring();    }   ... } 

the error nullpointerexception line: return actorrepository.save(a); in actorservice.insert() method.

any idea why happening?

edit: here spring configurations

@configuration public class springmongoconfig extends abstractmongoconfiguration {      @bean     public gridfstemplate gridfstemplate() throws exception      {         return new gridfstemplate(mongodbfactory(), mappingmongoconverter());     }      @override     protected string getdatabasename()      {         return "seaas";     }      @override     @bean     public mongo mongo() throws exception      {         return new mongoclient("localhost" , 27017 );     }      public @bean mongotemplate mongotemplate() throws exception      {         return new mongotemplate(mongo(), getdatabasename());     }     } 

the problem not using spring actorservice dependency -instead have manually instantiated dependency using

private actorservice actorservice = new actorservice();.

the following code easiest fix in order inject actorservice dependency controller.

@restcontroller public class controllers  {    private static final logger logger = logger.getlogger(controllers.class);   private static final applicationcontext ctx = new  annotationconfigapplicationcontext(springmongoconfig.class);    @autowired    private actorservice actorservice;    @requestmapping(value="/createactor", method=requestmethod.post)   public @responsebody string createactor(@requestparam(value = "fname") string fname,         @requestparam(value = "lname") string lname,         @requestparam(value = "role") string role)   {     return actorservice.insert(new actor(null,fname,lname,role)).tostring();    }   ... } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -