0
votes

I am getting a little bit lost in trying to integrate spring and jersey in my spring-data-neo4j unmanaged server plugin. I have created POJO models with neo4j annotations for persisting in the store. On top of that I have created spring-data-neo4j repositories for operations over the data. I created a springContext.xml file and placed it under the resources folder.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">

    <context:annotation-config />
    <context:spring-configured />

    <neo4j:repositories base-package="com.mycompany.poc.graph.repositories" /> 
    <context:component-scan base-package="com.mycompany.poc.rest" /> 
</beans>

Further, i have setup SpringPluginInitializer and initialized the springContext.xml.

import com.mycompany.poc.graph.repositories.NodeObjectGraphRepository;
public class SpringExtensionInitializer extends SpringPluginInitializer {

    @SuppressWarnings("unchecked")
    public SpringExtensionInitializer() {
        super(new String[] {
            "META-INF/spring/springContext.xml"
        }, expose("template", Neo4jTemplate.class), 
           expose("nodeObjectGraphRepository", NodeObjectGraphRepository.class),
           expose("pathController", PathController.class));        
    }

}

I have exposed business services through PathController a Jersey Rest end point.

package com.mycompany.poc.rest;
import com.mycompany.poc.graph.repositories.NodeObjectGraphRepository;

@Path("/path")
@Component
public class PathController {

    @Autowired NodeObjectGraphRepository nodeObjectGraphRepository;

    @GET
    @Path("/paths")
    @Produces(MediaType.APPLICATION_JSON)
    public Response paths(@QueryParam("startNode") Long startNodeId, @QueryParam("endNode") Long endNodeId) {
        Response response = null;
        if( startNodeId != null && endNodeId != null){
            try{
                EndResult endResult = nodeObjectGraphRepository.getPathsBetweenNodes(startNodeId, endNodeId);
                response = Response.ok().entity(endResult).build();

            }catch(Exception e){
                response = Response.serverError().entity(ExceptionUtils.getFullStackTrace(e)).build();
            }
        }else{
            response = Response.serverError().entity("Invalid Inputs").build();
        }        
        return response;
    }
}

I have annotated PathController class with Spring stereotypes "Component" and also injected "NodeObjectGraphRepository" object through Spring Autowiring. However, i get a null nodeObjectGraphRepository object and hence not able to use the repositories. In the springContext.xml, I have setup scan for spring data neo4j repositories and Jersey controller annotated with Spring stereotype "Component". I am not able to figure how to inject spring dependencies. Any help is very much appreciated.

1

1 Answers

1
votes

I solved it. Thanks to Tauren Mills. I followed his advice. Have to use @InjectParam instead of @Autowired. I was able to inject spring beans by using @InjectParam instead of @Autowired annotation. However, i am not able to inject Spring Data Neo4j Repositories. When i start Neo4j community server, i get error in the console log.

INFO: Root resource classes found:
  class com.mycompany.poc.rest.PathController
May 03, 2014 6:36:55 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
May 03, 2014 6:36:55 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.9 09/02/2011 11:17 AM'
May 03, 2014 6:36:56 AM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  **SEVERE: The class com.mycompany.poc.graph.repositories.NodeObjectGraphRepository is an interface and cannot be instantiated.**
06:36:56.397 [AWT-EventQueue-0] WARN  /extensions - unavailable
com.sun.jersey.spi.inject.Errors$ErrorMessagesException: null
    at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170) ~[neo4j-desktop-2.0.2.jar:2.0.2]
    at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136) ~[neo4j-desktop-2.0.2.jar:2.0.2]
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199) ~[neo4j-desktop-2.0.2.jar:2.0.2]