0
votes

Please any help on this

I would like to ask your help on this, I configured a project in Netbeans to build a Restful Webservice with Spring MVC and Hibernate to connect to the database.

The problem is that I don't know how to configure the controller to execute and return the JSON.

I already developed my Data Access Layer, my service and my controller, but I don't know how to configure the project to execute: http:localhost:8080/project/user for example.

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>

Error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [DataAccessLayer.DataAccessObject.UserDAO] 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)}org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)

org.springframework.beans.factory.BeanCreationException: Could not autowire field: private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [DataAccessLayer.DataAccessObject.UserDAO] 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)}

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [DataAccessLayer.DataAccessObject.UserDAO] 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)}

org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ServiceLayer.Service.UserService ServiceLayer.Controller.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [DataAccessLayer.DataAccessObject.UserDAO] 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)}

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ServiceLayer.Service.UserService ServiceLayer.Controller.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [DataAccessLayer.DataAccessObject.UserDAO] 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)}

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ServiceLayer.Service.UserService ServiceLayer.Controller.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private DataAccessLayer.DataAccessObject.UserDAO ServiceLayer.Service.UserService.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [DataAccessLayer.DataAccessObject.UserDAO] 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)}

I will appreciate a lot your help.

Project Structure

2
You have everything required, just run the project on a Servlet container like Tomcat and use Postman or any Rest Client to access the URL to see the response JSON. Note you might have to set Accepts and Content-Type headers to application/json .M4ver1k
Thanks @M4ver1k, I want to see the json in the browser, could you help me with the url structure to do that, because I tried but never get the result.Jose Raul Perera
for example I tried this: http://localhost:8080/SignaSafeProject/usercontroller/user/ and http://localhost:8080/SignaSafeProject/user, but I always get error 404Jose Raul Perera
http://localhost:8080/SignaSafeProject/usercontroller/user/ is not mapped to anything in above controller but http://localhost:8080/SignaSafeProject/user should work. Are u seeing any exception in the console on application startup ?M4ver1k
No i don't get any error.. I got this: HTTP Status 404 - /SignaSafeProject/user, I think I miss something in the web.xml or in any xml configurationJose Raul Perera

2 Answers

0
votes

This is not straight answer to your question. But it may help in longer run.

Why don't you make your application as spring boot application. It will make your application simpler. You don't need to configure web.xml, deploy your war and so on. May be you can get started with this Complete documentation can be found here

0
votes

You could always start the application in debug mode from the IDE and see the logs for which URL Mappings are being registered on to your controller handlers. On a different note :

  • Then, there is also this in the stacktrace : org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ServiceLayer.Service.UserService] found for dependency: expected at least 1 bean which qualifies which points to a possible configuration requirement for scanning components. So please adjust your <context:component-scan> tag to include the packages containing your beans.
    As an example : <context:component-scan base-package="ServiceLayer.Service, ServiceLayer.Controller, ServiceLayer.Dao" />

  • Also, your web.xml maps *.htm to your dispatcher, so if you want your dispatcher to receive all the requests - you might want to change that to / or /* based on how you want it to operate. Otherwise, only requests like http:localhost:8080/project/user.htm will be processed, and not the type of requests that you intend to process which is mentioned as this http:localhost:8080/project/user in the question.

HTH.