Status 404, kiedy odpalamy jara poza katalogiem głównym aplikacji

0

Próbuję zrobić deploy aplikacji Spring Boot MVC za pomocą jara. Tworzę jat komendą mvn. Kiedy jestm w katalogu głównym aplikacji, to jar działa i wyświetla się strona. Ale jak przeniosę tego jara do zupełnie innego katalogu, poza projekt, to po odpaleniu pokazuje się błąd 404 w przeglądarce.
Chciałbym zrozumieć, czemu się tak dzieje. Czy to wina aplikacji, czy niewłaściwie tworzę jara.

Sklonowałem tę aplikację z githuba: https://github.com/spring-guides/gs-rest-service-cors/tree/master/complete

( To jest w ogóle kod do jednego tutoriala Springa Boot )

Poniżej widać utworzenie jara, odpalenie jara, sprawdzenie, czy działa curlem. Widać, że działa. Jesteśmy w katalogu głównym aplikacji.


u@apu ~/gs-rest-service-cors/complete
$ ./mvnw clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.example:rest-service-cors >--------------------
[INFO] Building rest-service-cors 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ rest-service-cors ---
[INFO] Deleting /home/u/gs-rest-service-cors/complete/target
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ rest-service-cors ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /home/u/gs-rest-service-cors/complete/src/main/resources
[INFO] skip non existing resourceDirectory /home/u/gs-rest-service-cors/complete/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ rest-service-cors ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/u/gs-rest-service-cors/complete/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ rest-service-cors ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ rest-service-cors ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ rest-service-cors ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ rest-service-cors ---
[INFO] Building jar: /home/u/gs-rest-service-cors/complete/target/rest-service-cors-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.4.2:repackage (repackage) @ rest-service-cors ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.012 s
[INFO] Finished at: 2021-03-11T02:41:56+01:00
[INFO] ------------------------------------------------------------------------
u@apu ~/gs-rest-service-cors/complete (master=)



$ java -jar target/rest-service-cors-0.0.1-SNAPSHOT.jar &
[1] 2441
u@apu ~/gs-rest-service-cors/complete (master=)
$ 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2021-03-11 02:44:40.095  INFO 2441 --- [           main] c.e.r.RestServiceCorsApplication         : Starting RestServiceCorsApplication v0.0.1-SNAPSHOT using Java 1.8.0_241 on apu with PID 2441 (/home/u/gs-rest-service-cors/complete/target/rest-service-cors-0.0.1-SNAPSHOT.jar started by u in /home/u/gs-rest-service-cors/complete)
2021-03-11 02:44:40.100  INFO 2441 --- [           main] c.e.r.RestServiceCorsApplication         : No active profile set, falling back to default profiles: default
2021-03-11 02:44:41.645  INFO 2441 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-11 02:44:41.667  INFO 2441 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-11 02:44:41.667  INFO 2441 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-03-11 02:44:41.752  INFO 2441 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-11 02:44:41.752  INFO 2441 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1546 ms
2021-03-11 02:44:42.073  INFO 2441 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 02:44:42.224  INFO 2441 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: ServletContext resource [/index.html]
2021-03-11 02:44:42.429  INFO 2441 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-11 02:44:42.452  INFO 2441 --- [           main] c.e.r.RestServiceCorsApplication         : Started RestServiceCorsApplication in 2.954 seconds (JVM running for 3.697)

$ curl localhost:8080
2021-03-11 02:45:36.328  INFO 2441 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-03-11 02:45:36.328  INFO 2441 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-03-11 02:45:36.329  INFO 2441 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
<!DOCTYPE html>
<html>
    <head>
        <title>Hello CORS</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="hello.js"></script>
    </head>

    <body>
        <div>
            <p class="greeting-id">The ID is </p>
            <p class="greeting-content">The content is </p>
        </div>
    </body>
</html>
u@apu ~/gs-rest-service-cors/complete (master=)
$ 

Teraz zabijamy proces serwera, kopiujemy jara poza projekt i odpalamy jara. Curl pokazuje status 404:


$ kill `lsof -ti:8080`
2021-03-11 02:47:21.173  INFO 2441 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
u@apu ~/gs-rest-service-cors/complete (master=)
 

$ cp target/rest-service-cors-0.0.1-SNAPSHOT.jar ~/target/
[1]+  Exit 143                /home/u/opt/jdk/bin/java -jar target/rest-service-cors-0.0.1-SNAPSHOT.jar
u@apu ~/gs-rest-service-cors/complete (master=)
$ cd ~/target/
u@apu ~/target
$ curl localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused
u@apu ~/target
$ java -jar rest-service-cors-0.0.1-SNAPSHOT.jar &
[1] 2585
u@apu ~/target
$ 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2021-03-11 02:51:35.644  INFO 2585 --- [           main] c.e.r.RestServiceCorsApplication         : Starting RestServiceCorsApplication v0.0.1-SNAPSHOT using Java 1.8.0_241 on apu with PID 2585 (/home/u/target/rest-service-cors-0.0.1-SNAPSHOT.jar started by u in /home/u/target)
2021-03-11 02:51:35.649  INFO 2585 --- [           main] c.e.r.RestServiceCorsApplication         : No active profile set, falling back to default profiles: default
2021-03-11 02:51:37.352  INFO 2585 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-11 02:51:37.373  INFO 2585 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-11 02:51:37.373  INFO 2585 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-03-11 02:51:37.452  INFO 2585 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-11 02:51:37.452  INFO 2585 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1704 ms
2021-03-11 02:51:37.762  INFO 2585 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 02:51:38.047  INFO 2585 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-11 02:51:38.066  INFO 2585 --- [           main] c.e.r.RestServiceCorsApplication         : Started RestServiceCorsApplication in 2.999 seconds (JVM running for 3.741)


$ curl localhost:8080
2021-03-11 02:52:53.748  INFO 2585 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-03-11 02:52:53.748  INFO 2585 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-03-11 02:52:53.751  INFO 2585 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 3 ms
{"timestamp":"2021-03-11T01:52:53.825+00:00","status":404,"error":"Not Found","message":"","path":"/"}u@apu ~/target
$ 

Ta różnica zachowania jara zależy od tego, w którym katalogu odpalamy java -jar .... Działa dobrze tylko, jak odpalamy w katalogu głównym aplikacji.

Rozwikłanie tego jest mi potrzebne, by robić deploy aplikacji webowych.
Cały projekt widać na githubie, ale wkleję tu pom.xml, może komuś ułatwi oglądanie
Z góry dziękuję


<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>rest-service-cors</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>rest-service-cors</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

1

Teraz zabijamy proces serwera, kopiujemy jara poza projekt i odpalamy jara. Curl pokazuje status 404:

Screen mówi co innego - najpierw zabiłeś proces serwera, skopiowałeś jara, odpaliłeś curla (pokazał 404 bo zabiłeś proces) i dopiero na koniec odpaliłeś przeniesionego jara ponownie.

0
Pinek napisał(a):

Screen mówi co innego - najpierw zabiłeś proces serwera, skopiowałeś jara, odpaliłeś curla (pokazał 404 bo zabiłeś proces) i dopiero na koniec odpaliłeś przeniesionego jara ponownie.

Zrobiłem minimalną aplikację Spring Boot, żeby wykluczyć różne czynniki i od nowa wszystko zarejestrowałem. Błąd 404 znów jest. Teraz chyba lepiej pokazałem:

./public/index.html

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

./pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>static-web-site</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>static-web-site</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

./src/main/java/com/example/staticwebsite/StaticWebSiteApplication.java

package com.example.staticwebsite;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StaticWebSiteApplication {

	public static void main(String[] args) {
		SpringApplication.run(StaticWebSiteApplication.class, args);
	}
}

Teraz pokazuję kolejne kroki. Najpierw tworzę jara i odpalam go będąc w katalogu głównym aplikacji. Komenda jest z ampersandem na końcu, aby zaraz potem sprawdzić respond curlem:

u@apu:~/gs-static-web-site$ rm -rf target
u@apu:~/gs-static-web-site$ ./mvnw clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.example:static-web-site >---------------------
[INFO] Building static-web-site 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ static-web-site ---
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ static-web-site ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /home/u/gs-static-web-site/src/main/resources
[INFO] skip non existing resourceDirectory /home/u/gs-static-web-site/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ static-web-site ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/u/gs-static-web-site/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ static-web-site ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ static-web-site ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ static-web-site ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ static-web-site ---
[INFO] Building jar: /home/u/gs-static-web-site/target/static-web-site-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.4.2:repackage (repackage) @ static-web-site ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.746 s
[INFO] Finished at: 2021-03-11T11:20:57+01:00
[INFO] ------------------------------------------------------------------------
u@apu:~/gs-static-web-site$ java -jar  target/static-web-site-0.0.1-SNAPSHOT.jar & 
[1] 8930
u@apu:~/gs-static-web-site$ 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2021-03-11 11:21:51.399  INFO 8930 --- [           main] c.e.s.StaticWebSiteApplication           : Starting StaticWebSiteApplication v0.0.1-SNAPSHOT using Java 1.8.0_241 on apu with PID 8930 (/home/u/gs-static-web-site/target/static-web-site-0.0.1-SNAPSHOT.jar started by u in /home/u/gs-static-web-site)
2021-03-11 11:21:51.403  INFO 8930 --- [           main] c.e.s.StaticWebSiteApplication           : No active profile set, falling back to default profiles: default
2021-03-11 11:21:52.916  INFO 8930 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-11 11:21:52.938  INFO 8930 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-11 11:21:52.939  INFO 8930 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-03-11 11:21:53.019  INFO 8930 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-11 11:21:53.020  INFO 8930 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1527 ms
2021-03-11 11:21:53.320  INFO 8930 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 11:21:53.471  INFO 8930 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: ServletContext resource [/index.html]
2021-03-11 11:21:53.660  INFO 8930 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-11 11:21:53.683  INFO 8930 --- [           main] c.e.s.StaticWebSiteApplication           : Started StaticWebSiteApplication in 2.883 seconds (JVM running for 3.595)
u@apu:~/gs-static-web-site$ curl localhost:8080
2021-03-11 11:22:18.907  INFO 8930 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-03-11 11:22:18.907  INFO 8930 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-03-11 11:22:18.909  INFO 8930 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

u@apu:~/gs-static-web-site$ 

Jak widać powyżej, zawartość index.html została wyświetlona, czyli napis Lorem ipsum itd.

Teraz przenoszę tego jara fo katalogu ~/target i tam przechodzę przez cd


u@apu:~/gs-static-web-site$ rm -rf ~/target/*
u@apu:~/gs-static-web-site$ cp  target/static-web-site-0.0.1-SNAPSHOT.jar ~/target/
u@apu:~/gs-static-web-site$ cd ~/target/
u@apu:~/target$ ls -l
total 16636
-rw-r--r-- 1 u u 17032413 Mar 11 11:28 static-web-site-0.0.1-SNAPSHOT.jar
u@apu:~/target$ 


Teraz killuję proces serwera, uruchamiam java -jar... z ampersandem i sprawdzam respond curlem. Mam nadzieję zobaczyć Lorem ipsum itd.


u@apu:~/target$ kill `lsof -ti:8080`
u@apu:~/target$ 2021-03-11 11:41:09.985  INFO 8930 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
kill `lsof -ti:8080`
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
[1]+  Exit 143                /home/u/opt/jdk/bin/java -jar target/static-web-site-0.0.1-SNAPSHOT.jar  (wd: ~/gs-static-web-site)
(wd now: ~/target)
u@apu:~/target$ lsof -ti:8080
u@apu:~/target$ java -jar  static-web-site-0.0.1-SNAPSHOT.jar & 
[1] 9234
u@apu:~/target$ 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2021-03-11 11:42:50.865  INFO 9234 --- [           main] c.e.s.StaticWebSiteApplication           : Starting StaticWebSiteApplication v0.0.1-SNAPSHOT using Java 1.8.0_241 on apu with PID 9234 (/home/u/target/static-web-site-0.0.1-SNAPSHOT.jar started by u in /home/u/target)
2021-03-11 11:42:50.869  INFO 9234 --- [           main] c.e.s.StaticWebSiteApplication           : No active profile set, falling back to default profiles: default
2021-03-11 11:42:52.488  INFO 9234 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-11 11:42:52.510  INFO 9234 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-11 11:42:52.510  INFO 9234 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-03-11 11:42:52.610  INFO 9234 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-11 11:42:52.610  INFO 9234 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1605 ms
2021-03-11 11:42:52.942  INFO 9234 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 11:42:53.250  INFO 9234 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-11 11:42:53.269  INFO 9234 --- [           main] c.e.s.StaticWebSiteApplication           : Started StaticWebSiteApplication in 3.023 seconds (JVM running for 3.738)
u@apu:~/target$ curl localhost:8080
2021-03-11 11:43:13.206  INFO 9234 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-03-11 11:43:13.206  INFO 9234 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-03-11 11:43:13.209  INFO 9234 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 3 ms
{"timestamp":"2021-03-11T10:43:13.269+00:00","status":404,"error":"Not Found","message":"","path":"/"}u@apu:~/target$ 


I jak widać w ostatniej lini, jako respond jest json ze statusem 404. (Ten json w ostatniej lini to rezultat wywołania curla w 32 linii. Taki json pojawia się również w chrome.)

Czyli ten jar nie działa poza aplikacją i raczej samodzielnie nie zadziała na zdalnym serwerze.

0

Przepraszam ale co to jest niby /public/index.html? o_O I jaki to ma związek z tym jarem którego budujesz? Aplikacja którą budujesz NIE MA endpointu / więc jak na taki wchodzisz to dostajesz 404. Aplikacja ma tylko endpointy /greeting i /greeting-javaconfig (ta której repo linkowałeś)

Ja nadal nie rozumiem co ty próbujesz odpalić i co chcesz osiągnąć. Zróbmy tak:

  1. Podajesz link do githuba z tym co odpalasz. Bo teraz to są jakieś zgadywanki bo nie pokazałeś żadnego kontrolera i w ogóle nie wiadomo co robisz.
  2. Opisz dokładnie co robisz, jakim curlem uderzasz np.
  3. Opisz jasno czego się spodziewałeś
  4. Napisz co zamiast tego dostałeś
0
Shalom napisał(a):

Aplikacja ma tylko endpointy /greeting i /greeting-javaconfig (ta której repo linkowałeś)

Aplikacja, której repo linkowałem ma 2 endpointy, to prawda. Ale po jej odpaleniu działa też endpoint "/". Myślę, że to jest jakieś zachowanie defaultowe Springa. Zresztą tutorial (do którego na wstępie linkowałem) https://spring.io/guides/gs/rest-service-cors/ właśnie uwzględnia działanie tego endpointu "/".

No i ja, widząc, że tego endpointa nie ma w kontrolerze, wywaliłem kontroler. Wtedy jar wywołany przez java -jar xxx.jar z katalogu głównego aplikacji powodował, że index.html się wyświetlał w przeglądarce. Ale wywołany z innego katalogu - zgłaszał błąd 404.

W końcu ogarnąłem temat. Po prostu w tym jarze nie ma katalogu public i obok katalogu target musi być katalog public.
Ostatecznie działa mi takie coś, co na razie mi starczy:

u@apu:~/temp0311$ tree
.
├── public
│   └── index.html
└── target
    └── static-web-site-0.0.1-SNAPSHOT.jar

2 directories, 2 files
u@apu:~/temp0311$ java -jar  target/static-web-site-0.0.1-SNAPSHOT.jar & 
[1] 1729
u@apu:~/temp0311$ 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2021-03-11 12:12:07.657  INFO 1729 --- [           main] c.e.s.StaticWebSiteApplication           : Starting StaticWebSiteApplication v0.0.1-SNAPSHOT using Java 1.8.0_241 on apu with PID 1729 (/home/u/temp0311/target/static-web-site-0.0.1-SNAPSHOT.jar started by u in /home/u/temp0311)
2021-03-11 12:12:07.662  INFO 1729 --- [           main] c.e.s.StaticWebSiteApplication           : No active profile set, falling back to default profiles: default
2021-03-11 12:12:09.399  INFO 1729 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-11 12:12:09.431  INFO 1729 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-11 12:12:09.432  INFO 1729 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-03-11 12:12:09.605  INFO 1729 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-11 12:12:09.605  INFO 1729 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1798 ms
2021-03-11 12:12:09.884  INFO 1729 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 12:12:10.032  INFO 1729 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: ServletContext resource [/index.html]
2021-03-11 12:12:10.198  INFO 1729 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-11 12:12:10.225  INFO 1729 --- [           main] c.e.s.StaticWebSiteApplication           : Started StaticWebSiteApplication in 3.34 seconds (JVM running for 6.69)
u@apu:~/temp0311$ curl localhost:8080
2021-03-11 12:12:37.494  INFO 1729 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-03-11 12:12:37.494  INFO 1729 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-03-11 12:12:37.495  INFO 1729 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

u@apu:~/temp0311$ 


1

o_O Spring nigdy nie kończy zaskakiwać:

While this may not be a new revelation to those of you that have been following Spring Boot since the SpringOne announcement, there is one detail for which you may not be aware. Spring Boot will automatically add static web resources located within any of the following directories:

    /META-INF/resources/
    /resources/
    /static/
    /public/

Czyli faktycznie robi taki cyrk że wystawia automatycznie pod rootem zawartość takich katalogów jeśli są w CWD. WTF?!

1 użytkowników online, w tym zalogowanych: 0, gości: 1