2

I have the following project structure :

enter image description here

src/main/resources/META-INF/applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <context:annotation-config/>

  <context:component-scan base-package="com.bet.manager.services"/>

</beans:beans>

src/webapp/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext.xml</param-value>
  </context-param>

  <!-- Creates the Spring Container shared by all Servlets and Filters -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- Processes application requests -->
  <servlet>
    <servlet-name>bet-manager-api</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>bet-manager-api</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

And src/webapp/WEB-INF/spring/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <!-- Enables the Spring MVC @Controller programming model -->
  <annotation-driven/>

  <context:component-scan base-package="com.bet.manager.web"/>

  <mvc:default-servlet-handler/>

</beans:beans>

Everythink for me looks fine, except that when i put the war in tomcat container and call http://localhost:8080/bet-manager-api/hello i get 404. TestCotroller class :

@RestController
@RequestMapping(value = "/hello")
public class TestController {

@Autowired
private TestService testService;

@RequestMapping(method = RequestMethod.GET)
public void hello() {
    testService.doSomethink();
} 
}

. Also in the pom.xml in the build section i set the final name to "bet-manager-api". What im doing wrong? And also why theres is no logging ? I check all the logs in tomcat/logs/ but i dont found any important logs. Thanks

1 Answers1

0

Everything looks fine on configuration side, yes. Try adding <context:annotation-config/> in your src/webapp/WEB-INF/spring/applicationContext.xml but it shouldn't cause issues.

Miloš Milivojević
  • 5,219
  • 3
  • 26
  • 39
  • 1
    My guess is you're doing something wrong while deploying it, you're probably using the wrong context path. Double check that you're using the correct path. Also try running the app with mvn org.apache.tomcat.maven:tomcat7-maven-plugin:run (I'm assuming you're using Maven as a build tool) and see what happens. It's running fine for me. – Miloš Milivojević Jul 13 '16 at 13:27
  • 1
    I found whats wrong.. I forgot to set the packaging to war. Everything now works just fine ;) . Btw can you explain me why i META-INF/applicationContext.xml and WEB-INF/applicationContext.xml in separate files. I delete the META-INF folder and just add one line to scan for services in WEB-INF/applicationContext.xml and its working. – Zdravko Georgiev Jul 13 '16 at 14:29
  • 1
    You don't need to have two contexts per se, it's just a common practice since you can have multiple DispatcherServlets and each of those servlets will have its own separate web application context which cannot share state. But if you include a ContextLoaderListener (/META-INF one) you can have a global configuration with beans shared between any number of web configurations. Now, in your case, having only one configuration works perfectly fine, no need to separate them. – Miloš Milivojević Jul 13 '16 at 14:39
  • Take a look at this answer: https://stackoverflow.com/questions/18578143/about-multiple-containers-in-spring-framework/18580299#18580299 – Miloš Milivojević Jul 13 '16 at 14:42