java 如何调试在 spring mvc rest 中找不到的 404 资源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33887618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:14:21  来源:igfitidea点击:

How to debug 404 resource not found in spring mvc rest?

javamavenspring-restcontrollerspring-rest

提问by diwakarb

I have a sample spring rest mvc application which has the following java code:

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

SampleController.java

示例控制器.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

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>
        <Hymanson.version>2.6.2</Hymanson.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.Hymanson.core</groupId>
            <artifactId>Hymanson-databind</artifactId>
            <version>${Hymanson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.Hymanson.core</groupId>
            <artifactId>Hymanson-core</artifactId>
            <version>${Hymanson.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

<?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>

dispatcher-servlet.xml

调度程序-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="HymansonMessageConverter"
        class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter" />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="HymansonMessageConverter" />
            </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.MappingHymansonHttpMessageConverter"> 
        </bean> </list> </property> </bean> -->

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

    <mvc:annotation-driven />

</beans>

homepage is giving me the apache page, but once I try to access 127.0.0.1:8080/sample It throws me a 404 error. Logs are all silent about it. Not sure how to fix this.

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

回答by Omkar

You have to add the context pathin the url - localhost:8080/<context-path>/sample. Typically Context pathwould be your warfile name if you deploy the app on Tomcat. If your warfile name is helloworld.war, then the URL would be localhost:8080/helloworld/sample.

您必须context path在网址中添加- localhost:8080/<context-path>/sample。通常Context path是你的war文件名,如果你部署的应用程序Tomcat。如果您的war文件名是helloworld.war,那么 URL 就是localhost:8080/helloworld/sample.

If you are using Tomcatconfigured in Eclipse, you can set the context-pathin the Modules tab.

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

Ways to Deploy your application to Tomcatroot -

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

  1. You can do it by simply naming the warfile as ROOT.war

  2. In ROOT.xmlfile, you have to specify this configuration <Context docBase="pathToWarFile" path="" reloadable="true" />and make sure that your warfile is not in webappsfolder.

  1. 您可以通过简单地将war文件命名为ROOT.war

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

If you deploy the application to the tomcat root, then there is no need to specify the context path. You will get the homepage of your app with the url localhost:8080/. In your case, you can call the controller method with the url localhost:8080/sample.

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

回答by Grim

If you have "SLF4J: Defaulting to no-operation (NOP) logger implementation" message:

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

Add a dependency to simple-logger like this:

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

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

Otherwise

否则

It needs reverse search.

它需要反向搜索。

  1. Find what message you like to see. This articlegives me the hint that i need the message No mapping found for HTTP request with.
  2. Search for sources in the web like this.
  1. 找到您喜欢看到的消息。这篇文章给了我提示,我需要该消息No mapping found for HTTP request with
  2. 像这样在网络上搜索资源。

And here we have the line:

在这里,我们有一行:

if (pageNotFoundLogger.isWarnEnabled()) {
  pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + getRequestUri(request) +
      "] in DispatcherServlet with name '" + getServletName() + "'");
}
  1. Finally lookup for the class and add the logging-level.
  1. 最后查找该类并添加日志记录级别。

回答by gary69

Make sure you're scanning the packages your controllers are in. Otherwise spring won't create beans for your controllers and the paths they're listening for will return a 404.

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

回答by isobretatel

Place a breakpoint in

放置一个断点

org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getMatchingPattern(String pattern, String lookupPath).

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

You will see why there is no match for your request.

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

回答by asg

First thing is your request mapping should be - @RequestMapping("/sample") and not @RequestMapping("sample")

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

回答by Mohit

Your servlet URL mapping is incorrect. It should be /* instead of /.

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

Take a look at this SO question for difference. Difference between / and /* in servlet mapping url pattern

看看这个 SO question 的不同之处。 servlet 映射 url 模式中 / 和 /* 之间的区别