使用Neo4J自动布线弹簧REST Web服务
我正在尝试开发一个简单的REST WS,它连接到在本机上运行的嵌入式Neo4J数据库。首先,我得到了一个简单的GET操作,当服务被调用并且正常工作时,它返回一个简单的字符串。现在我试图让它真正做到这一点,所以我搜索了一些关于如何将Spring与Neo4J集成的教程。事实是,我发现这比我期待的要困难得多,主要是因为有很多教程,它们都看起来不一样,所以有100种不同的方式来做到这一点,我试图适应我的解。我不知道我的注释和我的XML配置文件。任何帮助是一个梦寐以求的。愿军队与我同在。 :)使用Neo4J自动布线弹簧REST Web服务
UPDATE_1:这是现在
<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>
的MyStore的servlet控制器
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));
}
}
资源库
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);
}
的服务
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
的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>
而现在有关自动装配Autowired对象错误。
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]
错误仍在继续,但我认为这是重要的部分。如果你需要查看日志错误的其他信息,请告诉我。
在此先感谢
您缺少XML配置文件中存储库的配置。事实上,据报道在Creating repository instances/XML Configuration在春季数据的Neo4j - 参考文献:
Each Spring Data module includes a repositories element that allows you to simply define a base package that Spring scans for you
现在,你需要添加添加到您的XML配置文件是什么下面的代码片段:
<jpa:repositories base-package="com.ruitex23.myStore.repositories" />
你还可以:
-
添加以下架构定义在
<beans...>
标签:xmlns:jpa="http://www.springframework.org/schema/data/jpa"
-
添加以下架构位置您
xsi:schemaLocation
:http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
架构定义是链接,但我也加入他们的答案一个完整的一个。我还将'repositories'标签修改为'jpa:repositories',因此它可以在您的配置和添加的模式定义中正常工作。 – araknoid
感谢您的答复araknoid,但仍然无法正常工作:我没有支持标记的模式定义,并且我相信那个JPA是您所记得的关系数据库,而Neo4J是GraphDB。 –
ruitex23
添加了模式定义,正如链接解释的那样,这是Neo4J Spring Data的配置和文档,因此它适用于GraphDB。 – araknoid