[英]Autowired Spring REST web service with Neo4J
我正在嘗試開發一個簡單的REST WS,該WS連接到在我的計算機上本地運行的嵌入式Neo4J DB。 首先,我得到了一個簡單的GET操作,只是在調用Service時返回一個簡單的String,並且該字符串正常工作。 現在,我試圖使它真正起作用,所以我搜索了一些有關如何將Spring與Neo4J集成的教程。 事實是,我發現這比我期望的要棘手得多,主要是因為有很多教程,而且它們看起來都不同,所以有大約100種不同的方式來做,而我正在努力適應解。 我不確定我的注釋以及XML配置文件。 給予任何幫助。 願原力與我同在。 :)
UPDATE_1 :現在是myStore servlet
<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>
控制器
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
<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>
現在,該錯誤與自動裝配對象有關。
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配置文件中缺少存儲庫的配置。 實際上,如在Spring Data Neo4j的“ 創建存儲庫實例/ XML配置”中所報告-參考文檔:
每個Spring Data模塊都包含一個repositories元素,可讓您簡單地定義Spring會為您掃描的基本軟件包
現在,您需要添加以下內容以添加到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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.