【问题标题】:Spring 4.2 dispatcher servlet 404 error [duplicate]Spring 4.2调度程序servlet 404错误[重复]
【发布时间】:2015-10-19 23:19:02
【问题描述】:

我使用 spring 4.2.2.RELEASE hibernate 5.0.2.Final 创建了一个应用程序,并成功地将这个应用程序部署到 jboss 为 7.1.1。我启用了日志级别 DEBUG,以便我可以看到正在发生的事情,并且根据日志,应用程序已成功部署。

war 文件的名称是 mycompany.war,因此它部署在上下文路径 /mycompany

我有一个像下面这样的控制器类

@RestController
public class AbcController {
@RequestMapping(value = "/campaigns", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
    public Campaign addCampaign(@RequestBody Campaign campaign) throws ServiceException {
        return campaignService.addCampaign(campaign);
    }
}

现在,当我使用邮递员将请求发送到 localhost:8080/mycompany/campaigns 时,我收到以下调试日志的 404 错误

04:30:18,975 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--127.0.0.1-8080-1) DispatcherServlet with name 'dispatcher' processing POST request for [/mycompany/campaigns]
04:30:18,975 WARN  [org.springframework.web.servlet.PageNotFound] (http--127.0.0.1-8080-1) No mapping found for HTTP request with URI [/mycompany/campaigns] in DispatcherServlet with name 'dispatcher'
04:30:18,976 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--127.0.0.1-8080-1) Successfully completed request

下面是web.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<web-app 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_3_0.xsd"
         version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

下面是application-context.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
    <tx:annotation-driven/>
    <context:component-scan base-package="com.mycompany"/>

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath*:META-INF/jpa-persistence.xml"/>
        <property name="persistenceUnitName" value="MainPU" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf"/>
    </bean>

    <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>

为什么我会遇到这个问题?我检查了完整的日志,日志中没有异常没有错误。我还可以看到我的控制器正在由 spring 初始化。

【问题讨论】:

  • 在 INFO 级别打开 Spring 日志并检查注册了哪些请求处理程序。
  • 我怀疑问题来自您的 Web 应用程序的上下文根。你是如何为 webapp 上下文根设置的?在 JBoss 中,通过 google 的简短检查,似乎取决于您的 EAR 的 application.xmlWEB-INF/jboss-web.xml 或 WAR 文件名。确保前 2 个不存在,以便您的上下文根是您的 WAR 名称。
  • 哦,发现另一个问题:我相信您应该在您的应用上下文 xml 中有 &lt;mvc:annotation-driven&gt;,但我似乎看不到它。
  • @AdrianShum 我必须添加 。现在它可以工作了,但这不是自动行为吗?
  • 不,不是,至少在你的情况下不是:)

标签: java spring spring-mvc servlets


【解决方案1】:

您在应用程序上下文 XML 中错过了&lt;mvc:annotation-driven/&gt;。例如,需要允许使用注解进行请求映射。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-09
    • 2014-02-01
    • 2015-09-01
    • 2014-04-03
    • 2011-08-16
    • 2015-11-05
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多