Spring 4.2调度程序servlet 404错误

时间:2015-10-19 23:19:03

标签: java spring spring-mvc servlets

我使用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>

为什么我要面对这个问题?我检查了完整的日志,日志中没有异常没有错误。另外我可以看到我的控制器在春天初始化了。

1 个答案:

答案 0 :(得分:3)

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