2016-07-13 97 views
2

我有以下项目结构:春季Web上下文路径配置

enter image description here

的src /主/资源/ 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/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> 

当我把战争在Tomcat容器,并呼吁http://localhost:8080/bet-manager-api/hello Everythink我看起来不错,但我得到404 TestCotroller类:

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

@Autowired 
private TestService testService; 

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

。同样在构建部分的pom.xml中,我将最终名称设置为“bet-manager-api”。什么即时做错了?还有为什么theres没有日志记录?我检查了tomcat/logs /中的所有日志,但没有发现任何重要的日志。谢谢

+0

你检查过哪些日志文件?你至少应该看到一些输出。你能粘贴日志文件内容吗? –

+0

catalina.out是服务器启动日志记录,在localhost_acess_log中是请求0:0:0:0:0:0:1 - [13/Jul/2016:15:04:01 +0300]“GET/bet-manager-api/hello HTTP/1.1“404 993 –

+0

catalina.out还应该包含Spring的启动日志 –

回答

0

一切看起来不错配置方面,是的。尝试在src/webapp/WEB-INF/spring/applicationContext.xml中添加<context:annotation-config/>,但它不应该导致问题。

+0

添加后仍然无法运行 –

+1

我的猜测是在部署它时出现错误,您可能正在使用错误的背景路径。仔细检查你是否使用了正确的路径。还可以尝试使用mvn org.apache.tomcat.maven运行应用程序:tomcat7-maven-plugin:run(我假设您使用Maven作为构建工具)并查看会发生什么。它对我来说运行良好。 –

+1

我发现最新的错误..我忘了将包装设置为战争。现在一切工作都很好;)。顺便说一句,你可以解释我为什么META-INF/applicationContext.xml和WEB-INF/applicationContext.xml在单独的文件中。我删除了META-INF文件夹,只需添加一行即可扫描WEB-INF/applicationContext.xml中的服务及其工作。 –