java - servlet.PageNotFound : No mapping found for HTTP request with URI in DispatcherServlet

标签 java spring jakarta-ee spring-mvc

<分区>

我正在使用 Spring MVC 创建 REST API 端点。 当我尝试访问 "http://localhost:8080/myapp/heroku/resources". 时出现 404 not fond 我在服务器日志中有这个: 警告 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>

网络.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 中呢?

关于java - servlet.PageNotFound : No mapping found for HTTP request with URI in DispatcherServlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24534298/

相关文章:

java - 文件并行流

java - 调整 JTextArea 的大小

java - 如何实现DB并发?

java - 无法在 IntelliJ Idea 2017.3.4 上使用 Maven 创建 RESTful Web 服务

java - 如何使用@WebListener部署java EE监听器

jsp - Struts 2 + 禁用表单自动完成

java - txt文件中的特殊字符

java - 在java中实现blowfish和RC5

java - 使用 hibernate/spring 一次加载所有惰性集合

spring - 使用 Spring Boot 的 Web API 通知