我想请求您的帮助,我在Netbeans
中配置了一个项目,用Spring MVC
和Hibernate
构建一个Restful Webservice来连接数据库。
问题是我不知道如何配置控制器来执行并返回JSON
。
我已经开发了我的数据访问层,我的服务和我的控制器,但我不知道如何配置要执行的项目:http:localhost:8080/project/user
。
调度-servlet.xml中
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<mvc:annotation-driven/>
<context:component-scan base-package="ServiceLayer.Controller"/>
</beans>
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[DataAccessLayer.DataAccessObject.UserDAO]类型的限定bean:预期至少有1个bean符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖的[DataAccessLayer.DataAccessObject.UserDAO]类型的限定bean:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
org.springframework.beans.factory.BeanCreationException:使用名称&#39; userService&#39;创建bean时出错:注册自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖的[DataAccessLayer.DataAccessObject.UserDAO]类型的限定bean:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private ServiceLayer.Service.UserService ServiceLayer.Controller.UserController.userService;嵌套异常是org.springframework.beans.factory.BeanCreationException:使用名称&#39; userService&#39;创建bean时出错:注册自动装配的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖的[DataAccessLayer.DataAccessObject.UserDAO]类型的限定bean:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
org.springframework.beans.factory.BeanCreationException:使用名称&#39; userController&#39;创建bean时出错:注册自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private ServiceLayer.Service.UserService ServiceLayer.Controller.UserController.userService;嵌套异常是org.springframework.beans.factory.BeanCreationException:使用名称&#39; userService&#39;创建bean时出错:注册自动装配的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖的[DataAccessLayer.DataAccessObject.UserDAO]类型的限定bean:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
org.springframework.beans.factory.BeanCreationException:使用名称&#39; org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0&#39;创建bean时出错。在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义:bean的初始化失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:使用名称&#39; userController创建bean时出错&#39 ;:自动连接依赖项的注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private ServiceLayer.Service.UserService ServiceLayer.Controller.UserController.userService;嵌套异常是org.springframework.beans.factory.BeanCreationException:使用名称&#39; userService&#39;创建bean时出错:注册自动装配的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖的[DataAccessLayer.DataAccessObject.UserDAO]类型的限定bean:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
我将非常感谢你的帮助。
答案 0 :(得分:0)
这不是你问题的直接答案。但它可能有助于长期运行。
为什么不将您的申请作为spring boot申请。它将使您的应用程序更简单。您不需要配置web.xml,部署战争等。也许你可以开始使用this 可以找到完整的文档here
答案 1 :(得分:0)
您始终可以从IDE以调试模式启动应用程序,并查看正在向控制器处理程序注册URL映射的日志。另外一点:
然后,stacktrace中还有这个:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ServiceLayer.Service.UserService] found for dependency: expected at least 1 bean which qualifies
,它指向扫描组件的可能配置要求。因此,请调整您的<context:component-scan>
标记,以包含包含您的bean的软件包
例如:<context:component-scan base-package="ServiceLayer.Service, ServiceLayer.Controller, ServiceLayer.Dao" />
此外,您的web.xml会将*.htm
映射到您的调度程序,如果您愿意
您的调度员将收到您可能想要的所有请求
根据您希望的操作方式将其更改为/
或/*
。
否则,只有http:localhost:8080/project/user.htm
之类的请求
将被处理,而不是您打算的请求类型
这个过程被提到http:localhost:8080/project/user
在问题中。
HTH。