I have the following code which attempts to save a POJO object (Actor) into MongoDB using Spring Mongo Repository, but the repository object is always Null. I have followed multiple examples but mainly 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 that uses the repository:
@Service
public class ActorService
{
@Autowired
private ActorRepository actorRepository;
public Actor insert(Actor a)
{
a.setId(null);
return actorRepository.save(a);
}
}
And I access the service from a 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 that I get is NullPointerException from this line: return actorRepository.save(a);
in the ActorService.insert()
method.
Any Idea why is this happening?
EDIT: Here is the 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 is that you are not using Spring to get the ActorService
dependency -instead you have manually instantiated the dependency using
private ActorService actorService = new ActorService();
.
The following code is the easiest fix in order to inject the ActorService
dependency into the 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();
}
...
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments