0

It has been 3 days since I started learning about Spring Framework and trying to implement a RESTful web service with MongoDB and Spring Framework. I am still at the beginning and trying to understand the configuration of Spring Framework. When I start my project and hit the desired URL it is not working. I have also upload my project to github (here is the url)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         id="WebApp_ID"
         version="2.4"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                             http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!-- Activates various annotations to be detected in bean classes -->
    <context:annotation-config />
    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
     For example @Controller and @Service. Make sure to set the correct base-package-->
    <context:component-scan base-package="shoponway.webservice" />
    <!-- Configures the annotation-driven Spring MVC Controller programming model.
    Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
    <mvc:annotation-driven />
    <!-- Loads MongoDB configuraton -->
    <import resource="mongo-config.xml" />
</beans>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:p="http://www.springframework.org/schema/p" 
       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">
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

and my controller

@Controller
public class PersonController {
    protected Logger logger = Logger.getLogger(PersonController.class);

    @Resource(name = "personService")
    private PersonService personService;

    @RequestMapping(value = "/allpersons", method = RequestMethod.GET)
    public String getAllPersons(Model model){
        model.addAttribute("persons", personService.getAllPersons());
        return "personspage";
    }
}

The error I am getting

 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:529)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:511)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4888)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
8
  • are you getting any error. is your context getting up properly ? Commented Mar 2, 2014 at 11:03
  • yes I insert the error by editing the question Commented Mar 2, 2014 at 11:17
  • Well, the error message is pretty clear: you did not define the "personService" bean in your context. Commented Mar 2, 2014 at 11:18
  • where can I define it and how? in applicationContext.xml or spring-servlet.xml? Commented Mar 2, 2014 at 11:20
  • You are using a very old version of Spring, and I recommend looking at the JavaConfig option and Spring Boot. They make configuration a lot simpler and are an easier starting point. Commented Mar 2, 2014 at 11:40

1 Answer 1

2

There are multiple problems in this example, which can be corrected as follows

  1. add @Service anotation to your shoponway.webservice.services.PersonService class, you need to add @Service for making it a proper candidate for injection.

  2. Your 'mongo-config.xml' is pointing to wrong mongo repository locations edit it to <mongo:repositories base-package="shoponway.webservice.services" /> from
    <mongo:repositories base-package="org.krams.tutorial.repositories" />

  3. Mongo template should refer to following bean <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> not with class="org.springframework.data.document.mongodb.MongoTemplate"

4) replace mongo-config.xml content with

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/data/mongo
      http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<mongo:mongo host="localhost" port="27017" />
<mongo:db-factory dbname="shoponwaydb" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>

 </beans>

`

Once your correct these , you will be able to run your example easily:

Sign up to request clarification or add additional context in comments.

19 Comments

I did everything you told. And I am still getting an error but at least this error trace is small. I insert new error trace.
check for spring-web.3.2.0.RELEASE jar in your target/WEB-INF/lib folder
Its running fine for me, with these changes only i am able to run this example. Please check the jars you was in your WEB-INF/lib folder
as I updated spring dependencies in pom, I have spring-web.4.0.2.RELEASE jar in target/WEB-INF/lib folder.
Really so weird. After I applied the changes you told, maven throws "Unable to configure" error. Altough I can see maven dependencies in project structure, I can not see them in Deployment Assembly
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.