Why can't Postman find my Spring REST API? - java

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"

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.

Field _________ in required a bean of type _________ that could not be found [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Working in intellij community edition.
How can i resolve this error. I have tried everything i could find on stackoverflow
Console
C:\Users\dell\.jdks\openjdk-17.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.1\lib\idea_rt.jar=60395:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\dell\OneDrive\Desktop\dailyWagers\target\classes;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-starter-data-jpa\2.6.3\spring-boot-starter-data-jpa-2.6.3.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-starter-aop\2.6.3\spring-boot-starter-aop-2.6.3.jar;C:\Users\dell\.m2\repository\org\aspectj\aspectjweaver\1.9.7\aspectjweaver-1.9.7.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.6.3\spring-boot-starter-jdbc-2.6.3.jar;C:\Users\dell\.m2\repository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;C:\Users\dell\.m2\repository\org\springframework\spring-jdbc\5.3.15\spring-jdbc-5.3.15.jar;C:\Users\dell\.m2\repository\jakarta\transaction\jakarta.transaction-api\1.3.3\jakarta.transaction-api-1.3.3.jar;C:\Users\dell\.m2\repository\jakarta\persistence\jakarta.persistence-api\2.2.3\jakarta.persistence-api-2.2.3.jar;C:\Users\dell\.m2\repository\org\hibernate\validator\hibernate-validator\6.2.0.Final\hibernate-validator-6.2.0.Final.jar;C:\Users\dell\.m2\repository\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;C:\Users\dell\.m2\repository\org\jboss\logging\jboss-logging\3.4.3.Final\jboss-logging-3.4.3.Final.jar;C:\Users\dell\.m2\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;C:\Users\dell\.m2\repository\org\springframework\data\spring-data-jpa\2.6.1\spring-data-jpa-2.6.1.jar;C:\Users\dell\.m2\repository\org\springframework\data\spring-data-commons\2.6.1\spring-data-commons-2.6.1.jar;C:\Users\dell\.m2\repository\org\springframework\spring-orm\5.3.15\spring-orm-5.3.15.jar;C:\Users\dell\.m2\repository\org\springframework\spring-context\5.3.15\spring-context-5.3.15.jar;C:\Users\dell\.m2\repository\org\springframework\spring-tx\5.3.15\spring-tx-5.3.15.jar;C:\Users\dell\.m2\repository\org\springframework\spring-beans\5.3.15\spring-beans-5.3.15.jar;C:\Users\dell\.m2\repository\org\slf4j\slf4j-api\1.7.33\slf4j-api-1.7.33.jar;C:\Users\dell\.m2\repository\org\springframework\spring-aspects\5.3.15\spring-aspects-5.3.15.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-starter-security\2.6.3\spring-boot-starter-security-2.6.3.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-starter\2.6.3\spring-boot-starter-2.6.3.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot\2.6.3\spring-boot-2.6.3.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.6.3\spring-boot-autoconfigure-2.6.3.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.6.3\spring-boot-starter-logging-2.6.3.jar;C:\Users\dell\.m2\repository\ch\qos\logback\logback-classic\1.2.10\logback-classic-1.2.10.jar;C:\Users\dell\.m2\repository\ch\qos\logback\logback-core\1.2.10\logback-core-1.2.10.jar;C:\Users\dell\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.1\log4j-to-slf4j-2.17.1.jar;C:\Users\dell\.m2\repository\org\apache\logging\log4j\log4j-api\2.17.1\log4j-api-2.17.1.jar;C:\Users\dell\.m2\repository\org\slf4j\jul-to-slf4j\1.7.33\jul-to-slf4j-1.7.33.jar;C:\Users\dell\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\dell\.m2\repository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;C:\Users\dell\.m2\repository\org\springframework\spring-aop\5.3.15\spring-aop-5.3.15.jar;C:\Users\dell\.m2\repository\org\springframework\security\spring-security-config\5.6.1\spring-security-config-5.6.1.jar;C:\Users\dell\.m2\repository\org\springframework\security\spring-security-web\5.6.1\spring-security-web-5.6.1.jar;C:\Users\dell\.m2\repository\org\springframework\spring-expression\5.3.15\spring-expression-5.3.15.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.6.3\spring-boot-starter-web-2.6.3.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.6.3\spring-boot-starter-json-2.6.3.jar;C:\Users\dell\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.13.1\jackson-databind-2.13.1.jar;C:\Users\dell\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.1\jackson-annotations-2.13.1.jar;C:\Users\dell\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.13.1\jackson-core-2.13.1.jar;C:\Users\dell\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.1\jackson-datatype-jdk8-2.13.1.jar;C:\Users\dell\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.1\jackson-datatype-jsr310-2.13.1.jar;C:\Users\dell\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.1\jackson-module-parameter-names-2.13.1.jar;C:\Users\dell\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.6.3\spring-boot-starter-tomcat-2.6.3.jar;C:\Users\dell\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.56\tomcat-embed-core-9.0.56.jar;C:\Users\dell\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.56\tomcat-embed-el-9.0.56.jar;C:\Users\dell\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.56\tomcat-embed-websocket-9.0.56.jar;C:\Users\dell\.m2\repository\org\springframework\spring-web\5.3.15\spring-web-5.3.15.jar;C:\Users\dell\.m2\repository\org\springframework\spring-webmvc\5.3.15\spring-webmvc-5.3.15.jar;C:\Users\dell\.m2\repository\mysql\mysql-connector-java\8.0.28\mysql-connector-java-8.0.28.jar;C:\Users\dell\.m2\repository\org\projectlombok\lombok\1.18.22\lombok-1.18.22.jar;C:\Users\dell\.m2\repository\org\springframework\spring-core\5.3.15\spring-core-5.3.15.jar;C:\Users\dell\.m2\repository\org\springframework\spring-jcl\5.3.15\spring-jcl-5.3.15.jar;C:\Users\dell\.m2\repository\org\springframework\security\spring-security-core\5.6.1\spring-security-core-5.6.1.jar;C:\Users\dell\.m2\repository\org\springframework\security\spring-security-crypto\5.6.1\spring-security-crypto-5.6.1.jar com.burrows.dailyWagers.DailyWagersApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.3)
2022-02-04 23:09:16.239 INFO 1328 --- [ main] c.b.dailyWagers.DailyWagersApplication : Starting DailyWagersApplication using Java 17.0.2 on DESKTOP-UNKQR00 with PID 1328 (C:\Users\dell\OneDrive\Desktop\dailyWagers\target\classes started by dell in C:\Users\dell\OneDrive\Desktop\dailyWagers)
2022-02-04 23:09:16.246 INFO 1328 --- [ main] c.b.dailyWagers.DailyWagersApplication : No active profile set, falling back to default profiles: default
2022-02-04 23:09:19.421 INFO 1328 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-02-04 23:09:19.586 INFO 1328 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 114 ms. Found 1 JPA repository interfaces.
2022-02-04 23:09:21.367 INFO 1328 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2022-02-04 23:09:21.408 INFO 1328 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-02-04 23:09:21.409 INFO 1328 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-02-04 23:09:21.621 INFO 1328 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-02-04 23:09:21.622 INFO 1328 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5248 ms
2022-02-04 23:09:21.765 WARN 1328 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controller': Unsatisfied dependency expressed through field 'workerService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.burrows.dailyWagers.service.WorkerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
2022-02-04 23:09:21.771 INFO 1328 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2022-02-04 23:09:21.818 INFO 1328 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-02-04 23:09:21.881 ERROR 1328 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field workerService in com.burrows.dailyWagers.controller.Controller required a bean of type 'com.burrows.dailyWagers.service.WorkerService' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.burrows.dailyWagers.service.WorkerService' in your configuration.
Process finished with exit code 1
Controller.java
package com.burrows.dailyWagers.controller;
import com.burrows.dailyWagers.model.Worker;
import com.burrows.dailyWagers.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
#RestController
#RequestMapping("/workers")
public class Controller {
#Autowired
private WorkerService workerService;
#GetMapping("/{workerType}")
public List<Worker> getWorkers(#PathVariable String workerType){
return this.workerService.getWorkers(workerType);
}
#GetMapping("/{id}")
public Worker getWorker(#PathVariable Long id){
return (Worker) this.workerService.getWorkers(String.valueOf(id));
}
#PostMapping("/{worker}")
public void postWorker(#PathVariable Worker worker){
this.workerService.postWorker(worker);
}
#PutMapping("/{worker}")
public void updateWorker(#PathVariable Worker worker){
this.workerService.updateWorker(worker);
}
#DeleteMapping("/{id}")
public ResponseEntity<HttpStatus> deleteWorker(#PathVariable Long id){
try {
this.workerService.deleteWorker(id);
return new ResponseEntity<>(HttpStatus.OK);
}
catch(Exception e){
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
WorkerService.java
package com.burrows.dailyWagers.service;
import com.burrows.dailyWagers.model.Worker;
import org.springframework.stereotype.Service;
import java.util.List;
#Service
public interface WorkerService {
public List<Worker> getWorkers(String workerType);
public Worker getWorker(Long id);
public void postWorker(Worker worker);
public void deleteWorker(Long id);
public void updateWorker(Worker worker);
}
WorkerServiceImpl
package com.burrows.dailyWagers.service;
import com.burrows.dailyWagers.model.Worker;
import com.burrows.dailyWagers.repository.WorkerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class WorkerServiceImpl implements WorkerService{
#Autowired
private WorkerRepository workerRepository;
public List<Worker> getWorkers(String workerType) {
return workerRepository.findAll();
}
public Worker getWorker(Long id) {
return workerRepository.getById(id);
}
public void postWorker(Worker worker) {
workerRepository.save(worker);
}
public void deleteWorker(Long id) {
Worker workerToBeDeleted = workerRepository.getById(id);
workerRepository.delete(workerToBeDeleted);
}
public void updateWorker(Worker worker) {
workerRepository.save(worker);
}
}
WorkerRepository.java
package com.burrows.dailyWagers.repository;
import com.burrows.dailyWagers.model.Worker;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface WorkerRepository extends JpaRepository<Worker,Long> {
}
Worker.java
package com.burrows.dailyWagers.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.time.LocalDate;
import java.util.List;
#Data
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Builder
public class Worker {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String workerType;
private String servicesOffered;
private LocalDate availability;
private Integer rating;
}
application.properties
server.port=8081
#mysql configuration
spring.datasource.url=jdbc:mysql://localhost:3306/maid-app-db
spring.datasource.username=root
spring.datasource.password=5856
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#hibernate configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
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.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.burrows</groupId>
<artifactId>dailyWagers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dailyWagers</name>
<description>daily wagers web application</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
</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>
I faced same issue when i was beginner in Spring Framework. If put #Service annotaion on interface then service interface become useless so, Remove #Service annotation from WorkerService and put this annotation in WorkerServiceImpl.

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

Java controller always return a 404 no matter what I do

I was following an example from the spring boot documentation and the java controller always returns a 404 for some reason
This is what I tried
package com.example.accessingdatamysql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller // This means that this class is a Controller
#RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
#Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
#PostMapping(path="/add") // Map ONLY POST Requests
public #ResponseBody String addNewUser (#RequestParam String name
, #RequestParam String email) {
// #ResponseBody means the returned String is the response, not a view name
// #RequestParam means it is a parameter from the GET or POST request
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
#GetMapping(path="/all")
public #ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
which is the example given by spring boot here https://spring.io/guides/gs/accessing-data-mysql/
Then I also tried
#RestController
#RequestMapping("/demo")
public class GreetingClass {
#GetMapping("/greeting")
public String getGreeting() {
return "hello";
}
}
.pom file:
<?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.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.braintobytes</groupId>
<artifactId>Finance_microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Finance_microservice</name>
<description>Finance service</description>
<properties>
<java.version>1.8</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-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</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>
I know this is weird, I set up everything as instructed on the page but apparently nothing works, and I also checked other questions similar to this none of them answer my question.
It does seem that the tomcat server is hit, because it says when I make a call
o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
but then I get with this call curl 'http://localhost:8080/demo/greeting' and with 'http://localhost:8080/demo/all'
curl : The remote server returned an error: (404) Not Found.
Logs (the file path is removed on the first line):
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.2.RELEASE)
2020-08-31 08:19:16.508 INFO 20708 --- [ main] c.b.f.FinanceMicroserviceApplication : Starting FinanceMicroserviceApplication on DESKTOP-A65224I with PID 20708 ()
2020-08-31 08:19:16.510 INFO 20708 --- [ main] c.b.f.FinanceMicroserviceApplication : No active profile set, falling back to default profiles: default
2020-08-31 08:19:17.096 INFO 20708 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-08-31 08:19:17.101 INFO 20708 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-31 08:19:17.101 INFO 20708 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-31 08:19:17.150 INFO 20708 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-31 08:19:17.150 INFO 20708 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 618 ms
2020-08-31 08:19:17.465 INFO 20708 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-31 08:19:17.675 INFO 20708 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-31 08:19:17.681 INFO 20708 --- [ main] c.b.f.FinanceMicroserviceApplication : Started FinanceMicroserviceApplication in 1.328 seconds (JVM running for 1.835)
2020-08-31 08:20:21.893 INFO 20708 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-08-31 08:20:21.893 INFO 20708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-08-31 08:20:21.905 INFO 20708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 12 ms
Response:
{
"timestamp": "2020-08-31T13:20:21.930+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/demo/greeting"
}
This works if I do this:
https://github.com/BraintoByte/Test_App_Stack/tree/master/demo
Different example:
I have attached a different example with hirarchy exactly the same but business logic omitted:
Code in controller:
package com.braintobytes.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.braintobytes.data.Currency;
import com.braintobytes.data.repository.CurrencyRepository;
#RestController
#RequestMapping("/demo")
public class CurrencyController {
#Autowired
private CurrencyRepository currencyRepository;
#GetMapping("/currency")
public String getCurrency() {
return "currency";
}
}
Project hierarchy:
Same exact pom and in application.properties:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mydatabase
spring.datasource.username=myusername
spring.datasource.password=mypassword
Your controller is in package - com.example.accessingdatamysql
Spring boot startup log shows your main application class c.b.f.FinanceMicroserviceApplication is in another non related package hierarchy.
Spring boot recommends to locate main application class in a root package above other classes so that the component scan works out of box.
If you keep package com.example as root package and move your main application class FinanceMicroserviceApplication to it then everything should work as expected.
If you don't follow Spring boot recommendation for package hierarchy then you need to explicitly specify packages to scan with #SpringBootApplication annotation.
Alright so none of the suggestions above worked, we were on the right path though.
Changing the structure as so worked:
There was also a pathing problem in the application.properties and thus I changed as so:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}
To:
jdbc:mysql://localhost
And finally:
package com.braintobytes.finance.data.repository;
import org.springframework.data.repository.CrudRepository;
import com.braintobytes.finance.data.Currency;
public interface CurrencyRepository extends CrudRepository<Currency, Integer> {
}
Woah!

Sprint-boot automatically shutting down after starting server

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>

Categories

Resources