servlet.PageNotFound:在DispatcherServlet中找不到带有URI的HTTP请求的映射

2020年12月2日 101点热度 0条评论

This question already has answers here:
Why does Spring MVC respond with a 404 and report “No mapping found for HTTP request with URI […] in DispatcherServlet”?

(9个答案)

3年前关闭。

我正在使用Spring MVC创建REST API端点。

尝试访问
"http://localhost:8080/myapp/heroku/resources".时我不喜欢404

我在服务器日志中有这个:


WARN servlet.PageNotFound:在DispatcherServlet 中未找到具有URI [/ myapp / heroku / resources]的HTTP请求的映射

 @Controller
 public class HerokuController {
 @RequestMapping(value = "/heroku/resources/", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
 public  ResponseEntity<JSONObject> provision(@RequestBody JSONObject body){
         JSONObject response = new JSONObject();
            JSONObject config_vars = new JSONObject();
            /*code*/
                    return new ResponseEntity<JSONObject>(response,HttpStatus.OK);  
    }

可能是什么问题?我确实使用

来扫描我的所有 Controller 。

谢谢

servlet-context.xml

<beans:import resource="classpath:spring-properties-loader.xml"/>

    <!-- To be able to aop controllers -->
    <aop:aspectj-autoproxy proxy-target-class="true" />

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- The HandlerMapping for JSONP requests. -->
    <beans:bean class="com.datapine.controllers.jsonp.JsonpHandlerMapping" />

    <!--    For uploading files -->
    <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <beans:property name="maxUploadSize" value="${csv.maxsize.file}"/>
    </beans:bean>

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
        <beans:property name="order">
            <beans:value>2</beans:value>
        </beans:property>
    </beans:bean>

    <context:component-scan
        base-package="com.datapine.download.controller, 
        com.datapine.controllers,
        com.datapine.controller
        com.datapine.payment.controller" />

    <!-- json -->
    <beans:bean id="xmlFileViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
        <beans:property name="location">
            <beans:value>/WEB-INF/views.xml</beans:value>
        </beans:property>
        <beans:property name="order">
            <beans:value>1</beans:value>
        </beans:property>
    </beans:bean>

web.xml

<?xml version="1.0"  encoding="UTF-8"?>
 <web-app version="3.0"  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">
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml
            classpath:spring-properties-loader.xml
            classpath:monitoring-spring.xml
            classpath:spring-security.xml
            classpath:domain-security.xml
            classpath:spring-mail.xml
            classpath:spring-context.xml
            classpath:smart_chart-db-config.xml
            classpath:logging-aop-context.xml
            classpath:salesforce-aop-context.xml
            classpath:jms-extractor-producer-activemq.xml
        </param-value>
      </context-param>
      <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>db_profile,db_mysql</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
      </listener> 
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
            /WEB-INF/spring/appServlet/servlet-context.xml
          </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>

      <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
          org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
      </filter>
      <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <servlet-name>appServlet</servlet-name>
      </filter-mapping>


      <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
           <param-name>encoding</param-name>  
           <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
           <param-name>forceEncoding</param-name>  
           <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping> 

        <!-- Monitoring -->
    <filter>
        <filter-name>monitoring</filter-name>
        <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
        <async-supported>true</async-supported>
        <!-- TO ENABLE LOGS -->
        <!-- <init-param> -->
        <!-- <param-name>log</param-name> -->
        <!-- <param-value>true</param-value> -->
        <!-- </init-param> -->
    </filter>
        <filter-mapping>
            <filter-name>monitoring</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <listener>
            <listener-class>net.bull.javamelody.SessionListener</listener-class>
        </listener>

    <!--   <filter> -->
    <!--        <filter-name>XssFilter</filter-name> -->
    <!--        <filter-class>com.datamint.utils.XssFilter</filter-class> -->
    <!-- </filter>    -->
    <!--    <filter-mapping> -->
    <!--        <filter-name>XssFilter</filter-name> -->
    <!--        <url-pattern>/*</url-pattern>        -->
    <!--    </filter-mapping> -->
</web-app>

包com.datapine.controllers.jsonp;

public 类JsonpHandlerMapping扩展了RequestMappingHandlerMapping {

private boolean useSuffixPatternMatch = true;

private boolean useTrailingSlashMatch = true;

@Override
protected boolean isHandler(final Class<?> beanType) {
    boolean result = true;
    result &= AnnotationUtils.findAnnotation(beanType, Jsonp.class) != null;
    result &= super.isHandler(beanType);
    return result;
}

@Override
protected RequestMappingInfo getMappingForMethod(final Method method,
        final Class<?> handlerType) {
    RequestMappingInfo info = null;
    RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(
            method, RequestMapping.class);
    final JsonpMethod methodJsonpAnnotation = AnnotationUtils
            .findAnnotation(method, JsonpMethod.class);
    final Jsonp jsonpAnnotation = AnnotationUtils.findAnnotation(
            handlerType, Jsonp.class);
    if (methodAnnotation != null && methodJsonpAnnotation != null) {
        RequestCondition<?> methodCondition = getCustomMethodCondition(method);
        info = createRequestMappingInfo(methodAnnotation, methodCondition,
                jsonpAnnotation);
        // RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(
        // handlerType, RequestMapping.class);
        // if (typeAnnotation != null) {
        // RequestCondition<?> typeCondition =
        // getCustomTypeCondition(handlerType);
        // info = createRequestMappingInfo(typeAnnotation, typeCondition,
        // jsonpAnnotation).combine(info);
        // }
    }
    return info;
}

private RequestMappingInfo createRequestMappingInfo(
        RequestMapping annotation, RequestCondition<?> customCondition,
        Jsonp jsonpAnnotation) {
    ArgumentUtil.assertTrue(annotation.method().length == 1);
    final String[] paths = annotation.value();
    final String pattern = jsonpAnnotation.value();
    final PatternsRequestCondition patterns = new PatternsRequestCondition(
            paths(paths, pattern), getUrlPathHelper(), getPathMatcher(),
            this.useSuffixPatternMatch, this.useTrailingSlashMatch);
    final RequestMethodsRequestCondition methods = new RequestMethodsRequestCondition(
            RequestMethod.GET);
    final String[] additionalParams;
    final RequestMethod methodMethod = annotation.method()[0];
    if (methodMethod == RequestMethod.GET) {
        additionalParams = new String[] { "callback",
                "_method=" + methodMethod.name() };
    } else {
        additionalParams = new String[] { "callback",
                "_method=" + methodMethod.name(), "_payload" };
    }
    final ParamsRequestCondition params = new ParamsRequestCondition(
            append(annotation.params(), additionalParams));
    final HeadersRequestCondition headers = new HeadersRequestCondition(
            annotation.headers());
    final ProducesRequestCondition produces = new ProducesRequestCondition(
            annotation.produces(), annotation.headers());
    final ConsumesRequestCondition consumes = new ConsumesRequestCondition(
            annotation.consumes(), annotation.headers());
    final RequestMappingInfo result = new RequestMappingInfo(patterns,
            methods, params, headers, consumes, produces, customCondition);
    return result;
}

private <T> T[] append(final T[] head, final T[] tail) {
    @SuppressWarnings("unchecked")
    final T[] result = (T[]) Array.newInstance(head.getClass()
            .getComponentType(), head.length + tail.length);
    System.arraycopy(head, 0, result, 0, head.length);
    System.arraycopy(tail, 0, result, head.length, tail.length);
    return result;
}

private String[] paths(final String[] values, final String pattern) {
    final String[] result = new String[values.length];
    for (int index = 0; index < values.length; ++index) {
        final String value = values[index];
        final String resultItem = pattern.replaceAll("\\{value\\}", value);
        result[index] = resultItem;
    }
    return result;
}

}

解决方案如下:

您可能会扫描 Controller (bean)两次,检查是否有两次扫描\定义了 Controller 。

顺便说一句:此 Controller 中的其他URL映射是否起作用?在其他 Controller 中呢?

上一篇:spring-integration - Spring TCP Client Server最大连接数

下一篇:jquery-ui - Zurb Foundation的jQuery UI主题?

相关文章:

java - 如何使用gradle将依赖项正确添加到另一个项目?

java - 用Java提供RESTful JSON API

spring - Spring Boot中的多个数据源和模式创建

web-services - 如何将Apache CXF Web服务部署到Glassfish

java - Spring @SessionAttributes返回null

java - Android Studio构建错误IllegalArgumentException

java - 在Java中读取上传的文件名

java - 如何在多线程中使用Spring事务

java - 在ManyToMany表,冬眠, Spring 中保存数据的问题

spring - modelMap.put()与modelMap.addAttribute()