spring - 404错误springboot restcontroller请求映射
问题描述
我有一个带有springboot(netbeans 8.2)的简单Web应用程序,但资源映射不起作用,我有2天的时间。有任何想法吗?
ApplicationContext://////////////////////////////////////// /
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/springXML8"/>
WEB.XML ////////////////////////////////////// /////////////
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>example</display-name>
<absolute-ordering />
<servlet>
<servlet-name>ServletCentral</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletCentral</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
ServletConfig://///////////////////////////////////// ///////////////
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- paquete a escanear en busca de componentes -->
<mvc:annotation-driven/>
</beans>
应用://////////////////////////////////////////////// //////////////
@SpringBootApplication 公共类 App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
控制器://////////////////////////////////////////////// ///////////////
@RestController
public class controller1 {
@RequestMapping("/")
public String hello(){
return "Lain love this service";
}
}
POM://////////////////////////////////////// ///////////////////
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>beans</groupId>
<artifactId>beans</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<name>SpringXML8</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
解决方案
@Diego Caballero
有许多选项会出现此错误。最常见的是 URL 的错误映射。
服务器可能会被这 2 个设置混淆,它可能不知道要尊重哪个上下文路径。验证您使用的 URL,尝试不同的选项、组合。
改变
<url-pattern>/*</url-pattern>
to
<url-pattern>/</url-pattern>
目前,您的映射 DispatcherServlet 被标记为处理所有请求,因为 /*。这意味着它还将尝试处理发送到 /WEB-INF/*** 的请求。它显然没有处理程序并且会失败。
模式 / 的特殊之处在于,如果没有其他模式匹配,它是默认的回退。如果有一个映射到请求路径的 Servlet,则将在映射的 DispatcherServlet 之前选择该 Servlet。(这可能是你的情况)。
尽量记录一切。log4j 的信息、错误和调试的使用级别。还尝试阅读服务器日志(位于您下载它的文件夹中)。不仅是您实际 IDE 的控制台。
验证您是否正确部署了应用程序。Maven 循环等 WAR 还是 JAR?
您是否使用正确的域?本地主机?或者可能是在服务器选项部署中设置的 ip 或不同的域
我希望它有点帮助。问候
推荐阅读
- python - 使用 matplotlib 看起来像平面 2D 数学插图的 3D 几何图?
- java - 在linux下使用分页响应(PagedResultsDirContextProcessor)使用Spring LDAP查询AD不起作用
- python - 如何在 colab 中导入 PyML?
- stripe-payments - Stripe APi paymentIntent 和 session 对象
- azure - Cosmos DB - 查找数组中项目列表的有效查询?
- sql - 我的 SQL Join 只产生了一半的正确聚合输出
- flask - 在谷歌云应用引擎中使用 Pyomo GLPK
- python - 将新电子邮件重定向到主页,以超链接格式跨越 autopygui win32 弹出窗口
- android-studio - Kotlin Android - 在 savedInstanceState 中保存 mutableMap?
- python - 在此代码中,您有一个门代码和一个键盘