Error 404, cuando intenta mostrar algo en tomcat
Hice todo en esto. video:
Pero cuando estoy escribiendo:http://localhost:8080/spring-sample-1.0-SNAPSHOT/hello
Este error aparece:
HTTP Status 404 – Not Found
Type Status Report
Message The requested resource [/spring-sample-1.0-SNAPSHOT/hello] is not available
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Lo único que cambié fue en: tomcat/bin/setclasspath.bat, He añadido una línea allí:
set JRE_HOME=C:\Program Files\Java\jre1.8.0_271
Porque sin eso el servidor no comienza
Okey, así que mi aplicación es muy simple, he creado el proyecto mvn en java 15, luego dos clases:
Config:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Configuration
@ComponentScan({"app"})
@EnableWebMvc
public class Config extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class[] getServletConfigClasses() {
return new Class[0];
}
@Override
protected String[] getServletMappings() {
return new String[0];
}
}
Hola:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@GetMapping("/hello")
public String get(){
return "Bycza zagroda!";
}
}
pom.xml:
4.0.0
pl.bykowski
spring-sample
1.0-SNAPSHOT
war
15
15
org.springframework
spring-webmvc
5.1.5.RELEASE
com.fasterxml.jackson.core
jackson-core
2.9.8
com.fasterxml.jackson.core
jackson-databind
2.9.8
javax.servlet
javax.servlet-api
4.0.1
provided
org.apache.maven.plugins
maven-war-plugin
3.2.3
false
Luego empaqué todo en el archivo de guerra y lo añadí en tomcat manager aquí: Archivo WAR para desplegar
Después de eso hice clic /spring-sample-1.0-SNAPSHOT en el módulo tomcat
y luego tecleo hola al final
¿Alguna idea de lo que va mal? :/
Pregunta hecha hace 3 años, 4 meses, 27 días - Por webweaverx
6 Respuestas:
-
Encontré el problema: en la clase Config usted proporcionó valores incorrectos para el mapeo de servlet y la clase de config de servlet. Por favor, cambie. Clase de Config como sigue:
@Configuration @ComponentScan({"app"}) @EnableWebMvc public class Config extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { return new Class[0]; } @Override protected Class[] getServletConfigClasses() { return new Class[] {Config.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
Respondida el Dec 20, 2020 a las 21:27 - por logicluminary3c42
Votos positivos: 0 | Votos negativos: 0 -
En primer lugar recomiendo configurar JAVA_ INICIO o JRE_HOME env variable para OS. Para más detalles: https://confluence.atlassian.com/doc/setting-the-java_home-variable-in-windows-8895.htmlEn cualquier caso cambiar los archivos de tomcat no es buena idea :)
Para resolver el problema con url inalcanzable, compruebe:
para el tomcat instalado:
- como regla, el nombre del archivo de guerra es el nombre de contexto y parte de la url(spring-sample-1.0-SNAPSHOT), pero ese nombre se puede configurar en context.xml (Documentación: https://tomcat.apache.org/tomcat-8.0-doc/config/context.html)
- open manager window as you can see in video (http://localhost:8080/manager/html) and find link of your web application
para el tomcat incrustado:
- el camino contextual debe ser como artefacto Id en pom.xml
Respondida el Dec 20, 2020 a las 21:36 - por gitguru
Votos positivos: 0 | Votos negativos: 0 -
@saver
Inicie sesión desde tomcat al desplegar:21-Dec-2020 16:49:22.227 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war] 21-Dec-2020 16:49:24.113 INFO [http-nio-8000-exec-17] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 21-Dec-2020 16:49:24.138 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war] has finished in [1,907] ms
¿Qué versiones de JRE y JDK debería usar?
Respondida el Dec 20, 2020 a las 21:44 - por devgeek
Votos positivos: 0 | Votos negativos: 0 -
@daniep kajoi debe establecer el camino en java 15 para tomcat, o cambiar maven.compiler.source atributo en pom.xml en versión 1.8 - una de dos opciones. Y veo en su registro que su camino es 'spring-sample3-1.0-SNAPSHOT.war '
21-Dec-2020 16:49:22.227 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war] 21-Dec-2020 16:49:24.113 INFO [http-nio-8000-exec-17] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 21-Dec-2020 16:49:24.138 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war] has finished in [1,907] ms
tratar de abrir url: http://localhost:8080/spring-sample3-1.0-SNAPSHOT.war/hello
Respondida el Dec 20, 2020 a las 21:49 - por compilerhero
Votos positivos: 0 | Votos negativos: 0 -
El error 404 que estás experimentando indica que Tomcat no puede encontrar la ruta especificada en tu aplicación.
Una posible causa de este problema es que en tu clase
Config
has sobrescrito el métodogetServletMappings()
y estás devolviendo un arreglo vacío. En lugar de eso, deberías devolver el patrón de URL al que debe responder tu controlador. Por ejemplo, si deseas que tu controladorHello
responda a la URL "/hello", deberías modificar tu claseConfig
de la siguiente manera:```java import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Configuration @ComponentScan("app") @EnableWebMvc public class Config extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[0]; }
@Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{Config.class}; // Aquí debes poner la clase de configuración actual } @Override protected String
Respondida el Dec 21, 2020 a las 21:27 - por Gpt
Votos positivos: 0 | Votos negativos: 0 -
La causa más probable del error es que no tienes un archivo
web.xml
en tu proyecto Spring Boot. Tomcat espera encontrar un archivoweb.xml
en la ruta raíz de tu aplicación web y lo utiliza para configurar el contexto de la aplicación web.Para resolver este problema, puedes agregar un archivo
web.xml
vacío a tu proyecto. Aquí tienes un ejemplo de un archivoweb.xml
vacío:xml
Guarda este archivo en la ruta raíz de tu proyecto, normalmente en
src/main/webapp
.Nota: Asegúrate de que el nombre del archivo sea
web.xml
y noweb.xml.EXAMPLE
.Después de añadir el archivo
web.xml
, vuelve a implementar tu aplicación en TomCat y comprueba si se resuelve el error.Respondida el Dec 21, 2020 a las 21:36 - por Gemini
Votos positivos: 0 | Votos negativos: 0