0

I'm trying to develop a simple REST WS that connects to an embedded Neo4J DB that runs locally on my machine. First I got a simple GET operation just returning a simple String when the Service was called and that works normally. Now I'm trying to get this to really do something so I've searched a couple of tutorials on how to integrate Spring with Neo4J. The truth is I've found this to be much more tricky than what I was expecting, mainly because there's a lot of tutorials and they all look different, so there's like 100 different ways to do it and I'm trying to adapt to my solution. I'm not sure on my Annotations and also on my XML configuration files. Any help is apreciatted. May the Force be with me. :)

UPDATE_1: this is myStore servlet now

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.0.xsd http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.ruitex23.myStore" />
<jpa:repositories base-package="com.ruitex23.myStore.repositories" />
<bean name="soapOperationRepository" class="com.ruitex23.myStore.services.SoapOperationService" />
<context:annotation-config />
</beans>

The Controller

package com.ruitex23.myStore.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ruitex23.myStore.domains.SoapOperation;
import com.ruitex23.myStore.services.SoapOperationService;

@RestController
public class SpringRestController {

@Autowired SoapOperationService operationService;

//the dummy method
@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
public String hello(@PathVariable String name) {
    String result = "Hallo " + name;
    return result;
}

//the real method
@RequestMapping(value = "/soapOperation/{version}", method = RequestMethod.GET)
public SoapOperation getSoapOperationByVersion(@PathVariable("version") Integer version) {
    return operationService.searchBySoapOperationVersion(String.valueOf(version));
}
}

The Repository

package com.ruitex23.myStore.repositories;
/**
* 
*/

import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.query.Param;

import com.ruitex23.myStore.domains.SoapOperation;


/**
* @author ruitex23
*
*/
public interface SoapOperationRepository extends GraphRepository<SoapOperation>  {

SoapOperation findBySoapOperationVersion(@Param("soapOperationVersion") String soapOperationVersion);

}

The Service

package com.ruitex23.myStore.services;

/**
* 
*/

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruitex23.myStore.domains.SoapOperation;
import com.ruitex23.myStore.repositories.SoapOperationRepository;

/**
* @author ruitex23
*
*/
@Service 
public class SoapOperationService {


@Autowired SoapOperationRepository soapOperationRepository;

public SoapOperation searchBySoapOperationVersion(String operationVersion) {
    return soapOperationRepository.findBySoapOperationVersion(operationVersion);
}

}

MyStore Servlet

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.0.xsd http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="com.ruitex23.myStore" />

//read somewhere that the interface should point to the impl
<bean name="soapOperationRepository" class="com.ruitex23.myStore.services.SoapOperationService" />
<context:annotation-config />

</beans>

web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>myStore</display-name>
<servlet>
    <servlet-name>mystore</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mystore</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

And now the error related to the Autowired object.

12:05:58.649 [main] WARN  o.s.w.c.s.XmlWebApplicationContext - Exception       encountered during context initialization - cancelling refresh attempt     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springRestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ruitex23.myStore.services.SoapOperationService com.ruitex23.myStore.controllers.SpringRestController.operationService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'soapOperationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ruitex23.myStore.repositories.SoapOperationRepository ruitex23.myStore.services.SoapOperationService.soapOperationRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ruitex23.myStore.repositories.SoapOperationRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) ~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE]

The error continues on and on, but I think this the important part. If u need to see anything else of the log error let me know.

Thank in advance

ruitex23
  • 1
  • 1
  • 4

1 Answers1

0

You are missing the configuration for the repositories in your XML configuration file. In fact, as reported in the Creating repository instances/XML Configuration at the Spring Data Neo4j - Reference Documentation:

Each Spring Data module includes a repositories element that allows you to simply define a base package that Spring scans for you

Now, what you need to add to add to your XML configuration file is the following snippet:

 <jpa:repositories base-package="com.ruitex23.myStore.repositories" />

You have also to:

  • add the following schema definition in your <beans...> tag:

    xmlns:jpa="http://www.springframework.org/schema/data/jpa"

  • add the following schema location to your xsi:schemaLocation:

    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd

araknoid
  • 3,065
  • 5
  • 33
  • 35
  • The schema definition was in the link, but I added also them in the answer for a complete one. I also modified the `repositories` tag to `jpa:repositories` so it will work fine with your configuration and the added schema definitions. – araknoid Oct 18 '17 at 13:15
  • Thanks for your reply araknoid but still doesn't work: I have no schema defintion supporting tag and I believe the one that does is for JPA which as you recall is a Relational DB while Neo4J is a GraphDB. – ruitex23 Oct 18 '17 at 13:16
  • Added schema definition and as the link explains, that is the configuration and documentation for Neo4J Spring Data, so it's suited for the GraphDB. – araknoid Oct 18 '17 at 13:20
  • Now I got confused. I did what you said, no errors were presented in Eclipse but when I try to test it I get this weird error: `15:38:23.369 [main] WARN o.s.b.f.xml.XmlBeanDefinitionReader - Ignored XML validation warning org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/data/jpa/spring-jpa.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .` – ruitex23 Oct 18 '17 at 13:39
  • This is another issue and you can find a proper solution at this solved [question](https://stackoverflow.com/questions/34656462/org-xml-sax-saxparseexception-schema-reference-4-failed-to-read-schema-documen) – araknoid Oct 18 '17 at 13:51
  • That link wouldn't be exactly what I call of "solved" as only has 1 unverified answer... Despite that I investigated other possible solutions and I feel I'm not getting much closer with this approach. Anyway, thanks a lot for your time @araknoid. – ruitex23 Oct 18 '17 at 22:38