【问题标题】:How to debug 404 resource not found in spring mvc rest?如何调试spring mvc rest中找不到的404资源?
【发布时间】:2015-11-24 07:22:23
【问题描述】:

我有一个示例 spring rest mvc 应用程序,它具有以下 java 代码:

SampleController.java

import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("sample")
public class SampleController {
        @RequestMapping(method = RequestMethod.GET, produces = "application/json")
        @ResponseBody
        public String getBatches()//@RequestParam(name = "name", required = true) String name)
        {
                return "Hello ";
        }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ved</groupId>
    <artifactId>platform</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>platform Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>4.2.1.RELEASE</spring.version>
        <jackson.version>2.6.2</jackson.version>
        <spring-boot.version>1.2.6.RELEASE</spring-boot.version>
        <filter.name>DEV</filter.name>
        <jersey.version>1.9</jersey.version>
        <base.directory>${basedir}</base.directory>
    </properties>
    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <property>
                    <name>env</name>
                    <value>local</value>
                </property>
            </activation>
            <properties>
                <filter.name>DEV</filter.name>
            </properties>
        </profile>
        <profile>
            <id>qa</id>
            <activation>
                <property>
                    <name>env</name>
                    <value>qa</value>
                </property>
            </activation>
            <properties>
                <filter.name>QA</filter.name>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <activation>
                <property>
                    <name>env</name>
                    <value>prod</value>
                </property>
            </activation>
            <properties>
                <filter.name>PROD</filter.name>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.8.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
    </dependencies>
    <build>
        <!-- <filters> <filter>${basedir}/src/main/resources/ENV-${filter.name}/application.properties</filter> 
            </filters> -->
        <finalName>platform</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <debug>true</debug>
                    <debuglevel>source,lines</debuglevel>
                    <showDeprecation>true</showDeprecation>
                    <archive>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <systemPropertyVariables>
                        <environment>prod</environment>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
                <artifactId>maven-svn-revision-number-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <entries>
                        <entry>
                            <prefix>svn</prefix>
                        </entry>
                    </entries>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <id>generate-timestamp</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>create-timestamp</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <format>{0,date,yyyy-MM-dd HH:mm}</format>
                    <items>
                        <item>timestamp</item>
                    </items>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</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>
</web-app>

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

    <!-- <context:property-placeholder location="classpath:application.properties" 
        /> -->

    <context:annotation-config />
    <context:component-scan base-package="com.ved.platform" />

    <bean id="jacksonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter" />
            </list>
        </property>
    </bean>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
        <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
        </bean> </list> </property> </bean> -->

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

    <mvc:annotation-driven />

</beans>

主页给了我 apache 页面,但是一旦我尝试访问 127.0.0.1:8080/sample 它就会抛出 404 错误。日志对此都保持沉默。不知道如何解决这个问题。

【问题讨论】:

  • 你解决了这个问题吗?如果是,您可以发布解决方案,以便对其他人有所帮助。

标签: java maven spring-restcontroller spring-rest


【解决方案1】:

中设置断点

org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getMatchingPattern(字符串模式,字符串查找路径)。

您将看到为什么您的请求不匹配。

【讨论】:

  • 帮了大忙。
【解决方案2】:

您必须在网址中添加context path - localhost:8080/&lt;context-path&gt;/sample。 如果您在Tomcat 上部署应用程序,通常Context path 将是您的war 文件名。如果您的war 文件名为helloworld.war,则URL 将为localhost:8080/helloworld/sample

如果您使用的是在Eclipse 中配置的Tomcat,您可以在“模块”选项卡中设置context-path

将应用程序部署到Tomcat root 的方法 -

  1. 您只需将war 文件命名为ROOT.war 即可完成此操作

  2. ROOT.xml 文件中,您必须指定此配置&lt;Context docBase="pathToWarFile" path="" reloadable="true" /&gt; 并确保您的war 文件不在webapps 文件夹中。

如果您将应用程序部署到tomcat 根目录,则无需指定context path。您将获得带有 URL localhost:8080/ 的应用主页。在您的情况下,您可以使用 url localhost:8080/sample 调用控制器方法。

【讨论】:

  • 这是我在 Tomcat/conf/Catalina/localhost 中的 ROOT.xml
  • 您是否在 Tomcat 上手动部署应用程序?如果有,是哪个版本?
  • 好的。在您的 ROOT.xml 中,而不是 docBase=platform,提供您的 war 文件的路径,然后重新启动服务器。.docBase='pathTowarFile' 并重新启动服务器
【解决方案3】:

确保您正在扫描您的控制器所在的包。否则 spring 不会为您的控制器创建 bean,并且它们正在侦听的路径将返回 404。

【讨论】:

  • 问:(关于 Spring Boot):如何确保 Spring Boot 正在扫描我们认为它应该扫描的控制器包?
  • Spring boot 会自动扫描同一个包中的类或者你的主类的子包中用@SpringBootApplication注解的类。否则你需要定义一个@ComponentScan 给它额外的包来扫描。
  • 问:那么没有办法获得 Spring Boot 已实际扫描的类的列表?问:完整的 URL 路径映射列表怎么样?
  • 我认为唯一的方法是打开调试日志并将它们传送到 bash 脚本 stackoverflow.com/questions/8543421/…
  • 很公平 - 谢谢。我问的原因是我已经打开了对 TRACE 的日志记录——还有很多“魔法”我想更好地理解。
【解决方案4】:

如果您有“SLF4J:默认为无操作 (NOP) 记录器实现”消息:

像这样向 simple-logger 添加依赖:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.5</version>
    </dependency>

否则

需要反向搜索。

  1. 找到您希望看到的消息。 This article 提示我需要消息 No mapping found for HTTP request with
  2. 在网络上搜索资源like this

这里有一行:

if (pageNotFoundLogger.isWarnEnabled()) {
  pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + getRequestUri(request) +
      "] in DispatcherServlet with name '" + getServletName() + "'");
}
  1. 最后查找类并添加日志级别。

【讨论】:

  • 有帮助,但是你的回答链接太多,感觉自己是考古学家!
  • @pdem 开发人员平均工资是 47k,考古学家是 58k。如果有人要求,我支持你加薪。
【解决方案5】:

首先,您的请求映射应该是 - @RequestMapping("/sample") 而不是 @RequestMapping("样本")

【讨论】:

    【解决方案6】:

    您的 servlet URL 映射不正确。它应该是 /* 而不是 /。

    看看这个 SO question 的区别。 Difference between / and /* in servlet mapping url pattern

    【讨论】:

      猜你喜欢
      • 2016-08-12
      • 2017-07-23
      • 1970-01-01
      • 2019-04-17
      • 2020-02-02
      • 2015-07-13
      • 2019-12-02
      • 1970-01-01
      • 2017-06-21
      相关资源
      最近更新 更多