繁体   English   中英

Spring和Hibernate Restful Web服务配置

[英]Spring and Hibernate Restful webservice configuration

请对此有任何帮助

我想在此方面寻求您的帮助,我在Netbeans配置了一个项目,以使用Spring MVCHibernate构建一个Restful Webservice来连接数据库。

问题是我不知道如何配置控制器以执行并返回JSON

我已经开发了数据访问层,服务和控制器,但是我不知道如何配置要执行的项目:例如http:localhost:8080/project/user

dispatcher-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。 依赖性注释:{@ 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可以作为此依赖项的自动装配候选。 依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

org.springframework.beans.factory.BeanCreationException:创建名称为'userService'的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可以作为此依赖项的自动装配候选。 依赖项注释:{@ 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:创建名称为'userService'的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可以作为此依赖项的自动装配候选。 依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

org.springframework.beans.factory.BeanCreationException:创建名称为'userController'的bean时出错:自动连接依赖项的注入失败; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private ServiceLayer.Service.UserService ServiceLayer.Controller.UserController.userService; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'userService'的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可以作为此依赖项的自动装配候选。 依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义的名称为'org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0'的bean创建时出错。豆失败; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'userController'的bean时出错:自动连接依赖项的注入失败; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private ServiceLayer.Service.UserService ServiceLayer.Controller.UserController.userService; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'userService'的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可以作为此依赖项的自动装配候选。 依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

非常感谢您的帮助。

项目结构

这不是您问题的直接答案。 但这可能有助于长期运行。

为什么不将您的应用程序制作为Spring Boot应用程序。 这将使您的应用程序更简单。 您无需配置web.xml,部署战争等。 也许您可以开始使用文档。完整的文档可以在这里找到

您始终可以从IDE以调试模式启动应用程序,并查看将URL映射注册到其上的日志到您的控制器处理程序。 在另一个说明上:

  • 然后,堆栈跟踪中也包含以下内容: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ServiceLayer.Service.UserService] found for dependency: expected at least 1 bean which qualifiesorg.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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM