簡體   English   中英

Spring Web上下文路徑配置

[英]Spring web context path configuration

我有以下項目結構:

在此處輸入圖片說明

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應用/ 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>

和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>

對我來說,Eve​​rythink看起來都不錯,但是當我將戰爭放入tomcat容器並調用http:// localhost:8080 / bet-manager-api / hello時,我得到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”。 我在做什么錯? 還有為什么沒有日志記錄呢? 我檢查了tomcat / logs /中的所有日志,但沒有找到任何重要的日志。 謝謝

在配置方面一切正常,是的。 嘗試在src / webapp / WEB-INF / spring / applicationContext.xml中添加<context:annotation-config/> ,但這不會引起問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM