Sprint-boot automatically shutting down after starting server - java

I am trying to create a simple Spring-boot app for practice. However it seems the server starts but then immediately shuts down. This problem has persisted for days and I'm unsure how to fix.
The entire console output I have displayed below. When I check localhost:8080 it shows nothing.
HelloSpringApplication.java
#SpringBootApplication
public class HelloSpringApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringApplication.class, args);
}
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</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>
</dependency>
</dependencies>
console
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.0.RELEASE)
2018-03-26 17:34:20.469 INFO 5561 --- [ main] com.example.demo.HelloSpringApplication : Starting HelloSpringApplication on Aarons-MacBook-Pro.local with PID 5561 (/Users/aaron/Documents/workspace-sts-3.9.3.RELEASE/HelloSpringApplication/target/classes started by aaron in /Users/aaron/Documents/workspace-sts-3.9.3.RELEASE/HelloSpringApplication)
2018-03-26 17:34:20.472 INFO 5561 --- [ main] com.example.demo.HelloSpringApplication : No active profile set, falling back to default profiles: default
2018-03-26 17:34:20.512 INFO 5561 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#691a7f8f: startup date [Mon Mar 26 17:34:20 CDT 2018]; root of context hierarchy
2018-03-26 17:34:21.021 INFO 5561 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-03-26 17:34:21.030 INFO 5561 --- [ main] com.example.demo.HelloSpringApplication : Started HelloSpringApplication in 0.743 seconds (JVM running for 1.141)
2018-03-26 17:34:21.032 INFO 5561 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#691a7f8f: startup date [Mon Mar 26 17:34:20 CDT 2018]; root of context hierarchy
2018-03-26 17:34:21.034 INFO 5561 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

That output looks similar to the output of using spring-boot-starter not spring-boot-starter-web which has an embedded tomcat server. Ensure that you are using that dependency in your current pom.xml. Then use mvn clean install to ensure the dependencies are installed correctly and then run the spring app with mvn spring-boot:run.
You can initialize a spring project with start.spring.io, this will provide a zip with a pre-configured empty project, don't forget to select Web dependency.

Ensure that the version of spring-boot-starter and spring-boot-web are compatible in the pom.xml

Just run the mvn clean install and mvn spring-boot:run and see if there is any stack trace at console which can help to find the reason.. Moreover verify pom.xml build config as
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Related

Connection refused trying to connect spring boot application in docker

I try to run spring boot application in Docker, Circle CI. Please, check how my .circleci/config.yml file looks like:
version: 2.1
jobs:
test:
docker:
- image: maven:3.8.5-openjdk-17-slim
environment:
CHROMEDRIVER_VERSION: 100.0.4896.60
CHROMEDRIVER_DIR: /tmp/chromedriver/
WIKI_ENV: CIRCLE_CI
steps:
- checkout
- run:
name: "Install gnupg2, wget, xvfb and unzip"
command: |
apt-get update
apt-get install -y gnupg2
apt-get install -y wget xvfb unzip
- run:
name: "Install Chrome Browser"
command: |
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
apt-get update -y
apt-get install -y google-chrome-stable
- run:
name: "Setup chromedriver"
command: |
mkdir -p $CHROMEDRIVER_DIR
wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR
export PATH=$CHROMEDRIVER_DIR:$PATH
- run:
name: "Build jar"
command: mvn clean package spring-boot:repackage
- run:
name: "Start"
command: mvn spring-boot:start
- run:
name: "Check"
command: curl http://localhost:8080
- store_test_results:
path: target/surefire-reports/junitreports
- store_artifacts:
path: target/surefire-reports/junitreports
- store_artifacts:
path: target/screenshots
workflows:
default:
jobs:
- test
A pipeline fails on a "Check" step with the curl: (7) Failed to connect to localhost port 8080: Connection refused error.
Here is the output of the "Start" step:
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for vznd:selenium:jar:1.0
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.testng:testng:jar -> duplicate declaration of version ${testng.version} # line 54, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ---------------------------< vznd:selenium >----------------------------
[INFO] Building selenium 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- spring-boot-maven-plugin:2.6.3:start (default-cli) # selenium ---
[INFO] Attaching agents: []
18:36:09.878 [Thread-0] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader#2c5fa29b
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.3)
2022-05-05 18:36:10.165 INFO 4520 --- [ restartedMain] vznd.selenium.SeleniumApp : Starting SeleniumApp using Java 17.0.2 on aeadb1d9da7e with PID 4520 (/root/project/target/classes started by root in /root/project)
2022-05-05 18:36:10.166 INFO 4520 --- [ restartedMain] vznd.selenium.SeleniumApp : No active profile set, falling back to default profiles: default
2022-05-05 18:36:10.239 INFO 4520 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-05-05 18:36:10.239 INFO 4520 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-05-05 18:36:11.021 INFO 4520 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-05-05 18:36:11.034 INFO 4520 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-05-05 18:36:11.035 INFO 4520 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-05-05 18:36:11.086 INFO 4520 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-05-05 18:36:11.086 INFO 4520 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 846 ms
2022-05-05 18:36:11.320 INFO 4520 --- [ restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2022-05-05 18:36:11.418 INFO 4520 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2022-05-05 18:36:11.459 INFO 4520 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-05-05 18:36:11.468 INFO 4520 --- [ restartedMain] vznd.selenium.SeleniumApp : Started SeleniumApp in 1.58 seconds (JVM running for 2.136)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.129 s
[INFO] Finished at: 2022-05-05T18:36:11Z
[INFO] ------------------------------------------------------------------------
Here is my 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 http://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.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>vznd</groupId>
<artifactId>selenium</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven-surefire-plugin.version>3.0.0-M6</maven-surefire-plugin.version>
<testng.version>7.5</testng.version>
<selenium-java.version>3.141.59</selenium-java.version>
<commons-io.version>2.11.0</commons-io.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium-java.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<webdriver.chrome.driver>/tmp/chromedriver/chromedriver</webdriver.chrome.driver>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
When I run this commands locally - it works. When it executes on CI - it fails. What I tried to fix:
Run java -jar target/selenium.jar & command on the "Start" step
Run mvn spring-boot:run & command instead of "Build jar" and "Start" steps
Run nohup mvn spring-boot:run & command instead of "Build jar" and "Start" steps
Add sleep 120 step after the "Start" step
Could you please advise me what I am doing wrong?
Pretty sure you are curl'ing before server even started, so try to wait for server to start with
- run:
name: wait
command: sleep 10
Moved to a gitlab CI, the problem is not actual anymore.

Why can't Postman find my Spring REST API?

I created a very basic Spring Boot project with REST APIs. I tried connecting it to my Angular app but it was getting some CORS security error so I switched to Postman. I'm trying to test it using Postman but I keep receiving a 404 not found error on Postman. Why am I not able to connect to my backend and post to tester function?
controller
package controllers;
#RestController
public class Handler {
#PostMapping("/tester/{id}")
public String tester(#PathVariable(value="id")long userid ){
System.out.println("in tester");
System.out.println(userid);
return "succeeded test";
}
}
main
package halloween.contest;
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
SecurityAutoConfiguration.class})
public class ContestApplication {
public static void main(String[] args) {
SpringApplication.run(ContestApplication.class, args);
}
}
application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
spring.data.rest.base-path=/
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.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>halloween</groupId>
<artifactId>contest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contest</name>
<description>halloween picture contest</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
console
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.6)
2021-10-26 15:44:12.823 INFO 18760 --- [ main] halloween.contest.ContestApplication : Starting ContestApplication using Java 1.8.0_144 on DESKTOP-VB80TS0 with PID 18760 (C:\Users\Sergio Rodríguez\Documents\Halloween\contest\target\classes started by Sergio Rodríguez in C:\Users\Sergio Rodríguez\Documents\Halloween\contest)
2021-10-26 15:44:12.827 INFO 18760 --- [ main] halloween.contest.ContestApplication : No active profile set, falling back to default profiles: default
2021-10-26 15:44:16.013 INFO 18760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-10-26 15:44:16.043 INFO 18760 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-26 15:44:16.043 INFO 18760 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-10-26 15:44:16.310 INFO 18760 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-26 15:44:16.310 INFO 18760 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3372 ms
2021-10-26 15:44:19.326 INFO 18760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-26 15:44:19.342 INFO 18760 --- [ main] halloween.contest.ContestApplication : Started ContestApplication in 7.201 seconds (JVM running for 7.946)
2021-10-26 15:45:45.138 INFO 18760 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-26 15:45:45.139 INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-10-26 15:45:45.157 INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 17 ms
equivalent cURL command
curl --location --request POST 'localhost:8080/tester/69'
Postman
Answering my own question now that I figured it out. It wasn't apparent based on the code I provided but I'll leave my post for future help (wording helps).
the problem was visibility of packages. I had my controller class in a different package from the main class.
To fix the CORS issue, next to #PostMapping("/tester/{id}") add #CrossOrigin(origins = "http://localhost:<port>") where port is the port from which your Angular app is running.
In your target URL, make sure you've included the default Spring Boot port of 8080, e.g. http://localhost:8080/tester/123 and obviously, please double check that you're doing a POST and not a GET.
got a similar problem..then added the below annotations on top of springbootappication where main() exists, then it's working fine for me.
#EnableWebMvc
#ComponentScan("com.*.*")
Also in postman make the below changes...
Disable/uncheck File --> Proxy --> "Use the system proxy"
Disable/uncheck File --> General --> "SSL Certificate Verification"

Whitelabel Error Page This application has no configured error view, so you are seeing this as a fallback

I am started building a spring boot application but i am getting this type of error "Whitelabel Error Page - This application has no configured error view, so you are seeing this as a fallback."
I created the project from https://start.spring.io/.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.3.RELEASE)
2020-09-09 16:12:00.389 INFO 19440 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on ***** with PID 19440 (C:\Users\m.petraglia\Desktop\demo\target\classes started by m.petraglia in C:\Users\m.petraglia\Desktop\demo)
2020-09-09 16:12:00.391 INFO 19440 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-09-09 16:12:01.024 DEBUG 19440 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2020-09-09 16:12:01.118 DEBUG 19440 --- [ main] o.s.w.r.r.m.a.ControllerMethodResolver : ControllerAdvice beans: none
2020-09-09 16:12:01.138 DEBUG 19440 --- [ main] o.s.w.s.adapter.HttpWebHandlerAdapter : enableLoggingRequestDetails='false': form data and headers will be masked to prevent unsafe logging of potentially sensitive data
2020-09-09 16:12:01.961 INFO 19440 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
2020-09-09 16:12:01.967 INFO 19440 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.837 seconds (JVM running for 2.132)
2020-09-09 16:12:07.977 DEBUG 19440 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [22183960-1] HTTP GET "/hello"
2020-09-09 16:12:07.998 DEBUG 19440 --- [ctor-http-nio-2] o.s.w.r.handler.SimpleUrlHandlerMapping : [22183960-1] Mapped to ResourceWebHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"]
2020-09-09 16:12:08.000 DEBUG 19440 --- [ctor-http-nio-2] o.s.w.r.resource.ResourceWebHandler : [22183960-1] Resource not found
2020-09-09 16:12:08.025 DEBUG 19440 --- [ctor-http-nio-2] org.springframework.web.HttpLogging : [22183960-1] Resolved [ResponseStatusException: 404 NOT_FOUND] for HTTP GET /hello
2020-09-09 16:12:08.031 DEBUG 19440 --- [ctor-http-nio-2] org.springframework.web.HttpLogging : [22183960-1] Writing "<html><body><h1>Whitelabel Error Page</h1><p>This application has no configured error view, so you (truncated)...
2020-09-09 16:12:08.042 DEBUG 19440 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [22183960-1] Completed 404 NOT_FOUND
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#GetMapping("/hello")
public String hello(#RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
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.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-webflux</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>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-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>
Project structure:
I don't understand what is the problem.
Try adding #RestController annotation to the class
maybe you should use html file to run it in browser in my opinion,and also if you use thymeleaf include the dependencies as well

Spring Boot Devtools how to reload Maven multiple dependent modules in the independent module?

I have a multiple modules Maven with this structure:
app-parent
-------app-library (Hibernate data entities layer)
-------app-main (contains app-library as dependency), Spring Boot web application.
The simple pom.xml files for these folders:
app-parent/pom.xml
<modules>
<module>app-library</module>
<module>app-main</module>
</modules>
<packaging>pom</packaging>
<name>app-parent</name>
app-library/pom.xml
<parent>
<groupId>test</groupId>
<artifactId>app-parent</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
.... Some libraries here ....
</dependencies>
app-main/pom.xml
<parent>
<groupId>test</groupId>
<artifactId>app-parent</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>petascope-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
With NetBeans 8.2 and Spring Boot version 1.5.2, I used Spring Devtools to auto reload changed Java classes file (~few seconds) instead of cold restart (> 10 seconds).
On app-main folder, I run this command to set up the WebApplication which allows NetBeans to attach a debugger to port 5005:
mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
Also, in application.properties of app-main/src/main/resources, I added this watch to allow Spring Devtools detect the change from app-library
spring.devtools.restart.additional-paths=../app-library
So, whenever I change one java file in app-main or app-library, I can see from the terminal that Spring DevTool does the reload in few seconds.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)
INFO [08:57:20] ApplicationMain#48: Starting ApplicationMain on localhost.localdomain with PID 19645 (/home/rasdaman/rasdaman_community/build/applications/petascope/target/petascope_main/classes started by rasdaman in /home/rasdaman/rasdaman_community/rasdaman/applications/petascope/petascope_main)
DEBUG [08:57:20] ApplicationMain#51: Running with Spring Boot v1.5.2.RELEASE, Spring v4.3.7.RELEASE
INFO [08:57:20] ApplicationMain#637: No active profile set, falling back to default profiles: default
[2018-05-01 08:57:22.341] - 19645 INFO [restartedMain] --- org.apache.catalina.core.StandardService: Starting service Tomcat
[2018-05-01 08:57:22.341] - 19645 INFO [restartedMain] --- org.apache.catalina.core.StandardEngine: Starting Servlet Engine: Apache Tomcat/8.5.11
[2018-05-01 08:57:22.361] - 19645 INFO [localhost-startStop-1] --- org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/rasdaman]: Initializing Spring embedded WebApplicationContext
INFO [08:57:24] ApplicationMain#57: Started ApplicationMain in 3.612 seconds (JVM running for 84.418)
The problem is with some basic change in a method of app-library, Spring DevTools can detect the file has been saved and reload, but from the app-main where called this method, the output is still the same, e.g:
app-library
public class Service {
public String returnValue() {
// return "Value before Spring DevTools reload.";
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// After app-main was set up from Maven command lines, I changed this line here and saved it to notice Spring DevTool to reload in the console.
return "Value after Spring DevTools reload."
}
}
app-main
public class TestService {
public TestService() {
Service service = new Service();
// !!!!!!!!!!!!!!!!!!!
// It can only print "Value before Spring DevTools reload."
// even though the Service file has been changed to return different value and Spring DevTools reloaded.
System.out.println(service.returnValue());
}
}
With this problem, I cannot just change in app-library and expect the changes will be applied in app-main. Instead, I have to stop the Maven command line, then in NetBeans click on app-main and select Build with dependencies, then runs Maven command line again to set up this Web Application with cold start (total: ~1 minute).
What can I do to make Spring DevTools can apply the changes from app-library to app-main instantly after I saved files in app-library? That would help to reduce the waiting time from NetBeans "Build with Dependencies".
So, in app-main it needs app-library built in local repository (~/.m2/...) and the only way to update this repository is going to app-library folder and run:
mvn install
After that, stop the running Web application from command line (ctrl + c) and restart it with same maven command on app-main folder:
mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
It still takes time to cold restart app-main, but it is acceptable.

Spring Boot App shutdown immediately under spring-boot-starter-parent version 2.0.1.RELEASE

I was studying JavaBrain's spring boot video and I ran into the same error as the link below.
Why does my Spring Boot App always shutdown immediately after starting?
I have specified the dependency of spring-boot-starter-web, but still got the error. My pom.xml looks like this. The thing is when I change the version of spring-boot-starter-parent to 1.4.2.RELEASE everything works fine(I can access the localhost:8080). I wonder why this is happening and what can I do to make version 2.0.1.RELEASE to work.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
The code looks like this:
#SpringBootApplication
public class CourseApiApp {
public static void main(String[] args) {
SpringApplication.run(CourseApiApp.class, args);
}
}
The output:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2018-04-16 21:42:07.576 INFO 8976 --- [ main] org.kouchibin.web.CourseApiApp : Starting CourseApiApp on SAI with PID 8976 (C:\Users\86660\Documents\workspace-sts\course-api\target\classes started by kcb in C:\Users\86660\Documents\workspace-sts\course-api)
2018-04-16 21:42:07.583 INFO 8976 --- [ main] org.kouchibin.web.CourseApiApp : No active profile set, falling back to default profiles: default
2018-04-16 21:42:07.670 INFO 8976 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#4c40b76e: startup date [Mon Apr 16 21:42:07 CST 2018]; root of context hierarchy
2018-04-16 21:42:08.971 INFO 8976 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-04-16 21:42:08.990 INFO 8976 --- [ main] org.kouchibin.web.CourseApiApp : Started CourseApiApp in 1.976 seconds (JVM running for 2.624)
2018-04-16 21:42:08.997 INFO 8976 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#4c40b76e: startup date [Mon Apr 16 21:42:07 CST 2018]; root of context hierarchy
2018-04-16 21:42:09.000 INFO 8976 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
I already solved the problem. When I used mvn compile manually, instead of clicking the RUN button in eclipse, more details showed up, where some of the dependencies cannot be resolved. So I checked those dependencies in the local repository. The problem is that some dependencies are not downloaded completely(with a ".inprogress" suffix). So the way to solve this is to delete all those broken dependencies and recompile the application, which will download the dependencies again. I guess this problem is most probably occurs in China, where the connection to the maven repository is not stable because of the Great Firewall. And why 1.4.2.RELEASE worked is simply because all the dependencies are downloaded successfully by chance.

Categories

Resources