【问题标题】:No mapping found for HTTP request with URI in DispatcherServlet with name 'dispatcher'. [duplicate]在名称为“dispatcher”的 DispatcherServlet 中找不到带有 URI 的 HTTP 请求的映射。 [复制]
【发布时间】:2017-05-01 09:30:51
【问题描述】:

我正在尝试迁移我们的应用程序以使用 Spring 4 功能。所以我正在慢慢尝试删除 XML 配置(web.xml、applicationContext.xml、dispatcher-servlet.xml)并用 Java 注释替换。我在这个问题上花了很多时间,我在 stackoverflow 中也找不到解决方案。我收到以下错误:

org.springframework.web.servlet.PageNotFound - [] [行号 - 1136] 在名为 'dispatcher' 的 DispatcherServlet 中未找到带有 URI [/WP/xhStatus] 的 HTTP 请求的映射。 对“http://localhost:8080/WP/xhStatus”的 POST 请求导致 404(空);调用错误处理程序 org.springframework.web.client.HttpClientErrorException: 404 null

从此错误消息中,我尝试查看以下 URL 的 Spring API 包 org.springframework.web.servlet 中是否有 PageNotFound,但没有此类或该软件包中可用的接口: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/package-summary.html

我只是在 WebAppInitializer.javaWebConfig.java 两个类下面添加,并从 删除 一些代码strong>web.xml 和 dispatcher-servlet.xml

WebAppInitializer.java

package com.mycompany.wp.web.config;

public class WebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
    applicationContext.setConfigLocations("com.mycompany.wp.web.config.WebConfig");
    servletContext.addListener(new ContextLoaderListener(applicationContext));

    servletContext.setInitParameter("contextConfigLocation", "/WEB-INF/applicationContext.xml");

    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(applicationContext));
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.addMapping("/");
}

}

WebConfig.java

package com.mycompany.wp.web.config;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.mycompany.wp")
public class WebConfig extends WebMvcConfigurerAdapter{

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class); 
    viewResolver.setPrefix("/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
   registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}

我从 web.xml删除这部分:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

我还从 dispatcher-servlet.xml删除了以下部分:

<mvc:resources mapping="/resources/**" location="/resources/" />

<context:component-scan base-package="com.mycompany.wp"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/views/" />
    <property name="suffix" value=".jsp" />
</bean>

现在dispatcher-servlet.xml只有一个条目:

<mvc:annotation-driven />

我的应用程序中仍然有所有 XML 文件(web.xml、dispatcher-servlet.xml 和 applicationContext.xml),因为它们还有一些其他信息。但是,applicationContext.xml 没有变化。

我正在尝试调用请求映射为 /xhStatus 的 REST 服务(Spring MVC 控制器),但我没有更改任何内容。之前,我的 REST 服务可以通过上述 URL 访问,但现在我无法访问。

在迁移到 Java 配置之前,我的应用程序过去可以很好地使用 XML 配置。可能 DispatcherServlet 找不到我的 Spring Controller。

如果有人可以帮助我,那就太好了。谢谢。

【问题讨论】:

    标签: spring rest spring-mvc servlets servlet-3.0


    【解决方案1】:

    我解决了这个问题。我在 WebAppInitializer.java

    中犯了一个错误

    我用过:

    applicationContext.setConfigLocations("com.mycompany.wp.web.config.WebConfig");
    

    现在我将该行替换为:

    applicationContext.register(com.mycompany.wp.web.config.WebConfig.class);
    

    效果很好。

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      相关资源
      最近更新 更多