Spring Boot Application unable to start Tomcat Server - java

I am using Spring Tool Suite to develop a simple "Spring starter project" (spring-boot) that returns a string on receiving a get request.
The project builds successfully but instead of starting the Tomcat server, it finishes executing.
I tried running it from command line using Maven plugin and as a packaged application(jar file) but the result is same.
I am new to spring-boot and tried to follow various tutorials and searched a lot for the solution but couldn't find anything. Here's my code and console output:
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>
<groupId>com.hraichandani</groupId>
<artifactId>starter-app-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>starter-app-2</name>
<description>Spring Boot Starter App</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.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>
<tomcat.version>8.5.23</tomcat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-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>
application.java
#SpringBootApplication
#RestController
public class StarterApp2Application {
public static void main(String[] args) {
SpringApplication.run(StarterApp2Application.class, args);
}
#RequestMapping(value="/greeting", method=RequestMethod.GET)
public String sayHello() {
return "Hello";
}
}
Console output
2017-11-27 04:05:49.881 INFO 17685 --- [ main] com.hraichandani.StarterApp2Application : Starting StarterApp2Application on Hiteshs-MacBook-Pro.local with PID 17685 (/Users/hitesh/Documents/spring-workspace/starter-app-2/target/classes started by hitesh in /Users/hitesh/Documents/spring-workspace/starter-app-2)
2017-11-27 04:05:49.884 INFO 17685 --- [ main] com.hraichandani.StarterApp2Application : No active profile set, falling back to default profiles: default
2017-11-27 04:05:49.916 INFO 17685 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#76b10754: startup date [Mon Nov 27 04:05:49 PST 2017]; root of context hierarchy
2017-11-27 04:05:50.838 INFO 17685 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-11-27 04:05:50.842 INFO 17685 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2017-11-27 04:05:50.891 INFO 17685 --- [ main] com.hraichandani.StarterApp2Application : Started StarterApp2Application in 11.226 seconds (JVM running for 16.758)
2017-11-27 04:05:50.892 INFO 17685 --- [ Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#76b10754: startup date [Mon Nov 27 04:05:49 PST 2017]; root of context hierarchy
2017-11-27 04:05:50.895 INFO 17685 --- [ Thread-3] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0
2017-11-27 04:05:50.897 INFO 17685 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Any help is greatly appreciated. Thanks!

Related

Can't Start the Spring Boot Application

I am trying to connect the database in my spring boot project. I have already created a database using phpMyAdmin. The MySQL server is running on port 3307.
But I couldn't run the application.
Here are the related files
pom.xml file is as following
<?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>3.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>LMS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LMS</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</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>
application.properties file is as following
spring.datasource.url=jdbc:mysql://localhost:3307/lms?useUnicode=true&useJDBCCompliantTimezone
Shift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
server.error.whitelabel.enabled=false
server.port=8080
spring.datasource.username=root
spring.datasource.password=pasindu
spring.main.web-application-type=none
spring.jpa.open-in-view=false
spring.thymeleaf.cache=false
api.base.path = http://localhost:8080
Here are the Logs in Spring Boot console
2022-12-08T00:23:21.986+05:30 INFO 27872 --- [ main] com.example.LMS.LmsApplication : Starting LmsApplication using Java 17.0.4.1 with PID 27872 (C:\Users\Pasindu_112247\eclipse-workspace\LMS.zip_expanded\LMS\target\classes started by Pasindu_112247 in C:\Users\Pasindu_112247\eclipse-workspace\LMS.zip_expanded\LMS)
2022-12-08T00:23:21.990+05:30 INFO 27872 --- [ main] com.example.LMS.LmsApplication : No active profile set, falling back to 1 default profile: "default"
2022-12-08T00:23:22.500+05:30 INFO 27872 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-12-08T00:23:22.568+05:30 INFO 27872 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 57 ms. Found 1 JPA repository interfaces.
2022-12-08T00:23:22.950+05:30 INFO 27872 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-12-08T00:23:23.157+05:30 INFO 27872 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl#3f0d6038
2022-12-08T00:23:23.160+05:30 INFO 27872 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-12-08T00:23:23.214+05:30 INFO 27872 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-12-08T00:23:23.260+05:30 INFO 27872 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.1.5.Final
2022-12-08T00:23:23.410+05:30 WARN 27872 --- [ main] org.hibernate.orm.deprecation : HHH90000021: Encountered deprecated setting [javax.persistence.sharedCache.mode], use [jakarta.persistence.sharedCache.mode] instead
2022-12-08T00:23:23.636+05:30 INFO 27872 --- [ main] SQL dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2022-12-08T00:23:24.274+05:30 INFO 27872 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-12-08T00:23:24.282+05:30 INFO 27872 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-12-08T00:23:24.787+05:30 INFO 27872 --- [ main] com.example.LMS.LmsApplication : Started LmsApplication in 3.204 seconds (process running for 3.63)
2022-12-08T00:23:24.794+05:30 INFO 27872 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-12-08T00:23:24.799+05:30 INFO 27872 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2022-12-08T00:23:24.811+05:30 INFO 27872 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
How do I fix this?

Use Spring Boot get entity from Oracle DataBase not work

I want to use Spring boot get entity from Oracle DB(Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production)
when i use repository findById method and toString in Console, it only return like this
LogReturnEmail [createDtm=, fileName=, forwardResult=, forwardTo=, id=f6a70923-973a-47ac-98a0-34200d4ddacb, mailSubject=, messageNo=, messageType=, sellerCompid=]
only id column have value.but i want to get all value.
Main class
#SpringBootApplication
public class Demo2Application implements ApplicationRunner {
#Autowired
private LogReturnEmailRepository repo2;
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
#Override
public void run(ApplicationArguments args) throws Exception {
Optional<LogReturnEmail> xml = repo2.findById("f6a70923-973a-47ac-98a0-34200d4ddacb");
System.out.println(xml.get());
}
}
Repository
#Repository
public interface LogReturnEmailRepository extends JpaRepository<LogReturnEmail, String> {
}
LOG_RETURN_EMAIL entity
/**
* The persistent class for the LOG_RETURN_EMAIL database table.
*
*/
#Entity
#Table(name="LOG_RETURN_EMAIL")
#NamedQuery(name="LogReturnEmail.findAll", query="SELECT l FROM LogReturnEmail l")
public class LogReturnEmail implements Serializable {
private static final long serialVersionUID = 1L;
#Column(name="CREATE_DTM")
private String createDtm;
#Column(name="FILE_NAME")
private String fileName;
#Column(name="FORWARD_RESULT")
private String forwardResult;
#Column(name="FORWARD_TO")
private String forwardTo;
#Id
#Column(name="ID")
private String id;
#Column(name="MAIL_SUBJECT")
private String mailSubject;
#Column(name="MESSAGE_NO")
private String messageNo;
#Column(name="MESSAGE_TYPE")
private String messageType;
#Column(name="SELLER_COMPID")
private String sellerCompid;
//commented out get/set method for brevity
hibernate configurations
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.Oracle9iDialect
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
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.1.4.RELEASE</version>
<relativePath />
</parent>
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo2</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
console log
2019-05-30 09:12:53.167 INFO 13668 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.3.9.Final}
2019-05-30 09:12:53.168 INFO 13668 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-05-30 09:12:53.241 INFO 13668 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-05-30 09:12:53.325 INFO 13668 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle9iDialect
2019-05-30 09:12:53.749 INFO 13668 --- [ restartedMain] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
2019-05-30 09:12:53.783 INFO 13668 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-05-30 09:12:54.006 INFO 13668 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-05-30 09:12:54.020 INFO 13668 --- [ restartedMain] com.example.demo.Demo2Application : Started Demo2Application in 2.115 seconds (JVM running for 2.691)
Hibernate:
select
logreturne0_.id as id1_0_0_,
logreturne0_.create_dtm as create_dtm2_0_0_,
logreturne0_.file_name as file_name3_0_0_,
logreturne0_.forward_result as forward_result4_0_0_,
logreturne0_.forward_to as forward_to5_0_0_,
logreturne0_.mail_subject as mail_subject6_0_0_,
logreturne0_.message_no as message_no7_0_0_,
logreturne0_.message_type as message_type8_0_0_,
logreturne0_.seller_compid as seller_compid9_0_0_
from
log_return_email logreturne0_
where
logreturne0_.id=?
LogReturnEmail [createDtm=, fileName=, forwardResult=, forwardTo=, id=f6a70923-973a-47ac-98a0-34200d4ddacb, mailSubject=, messageNo=, messageType=, sellerCompid=]
2019-05-30 09:12:54.079 INFO 13668 --- [ Thread-10] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-05-30 09:12:54.080 INFO 13668 --- [ Thread-10] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2019-05-30 09:12:54.088 INFO 13668 --- [ Thread-10] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
TABLE COLUMN NAME
ID
SELLER_COMPID
MESSAGE_NO
MAIL_SUBJECT
CREATE_DTM
FORWARD_TO
FORWARD_RESULT
FILE_NAME
MESSAGE_TYPE
Verify your Java Entity class datatypes and Oracle DATA_TYPE. This issue mostly occurs when data type mismatch.

Spring boot REST API 404 error

I am trying to create a basic spring boot application (JDK 1.8) with a REST API .
The following is my application code
package org.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan("org.demo")
#EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
I have added a simple controller as follows
package org.demo.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
#RestController(value = "/hello")
public class HelloController {
#GetMapping
#ResponseBody
public String hello() {
return "Hello";
}
}
The POM file of my project is as follows
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
When I run this application, it starts successfully. However, when I try to hit the URL localhost:8080 or loclahost:8080/hello, I get the 404 error message
The following are the startup logs
:: Spring Boot :: (v2.0.1.RELEASE)
2018-04-24 21:51:38.888 INFO 12068 --- [ main] org.demo.DemoApplication : Starting DemoApplication on DESKTOP-G2QR23G with PID 12068 (I:\demo\spring-boot\demo\target\classes started by chirayu in I:\demo\spring-boot\demo)
2018-04-24 21:51:38.892 INFO 12068 --- [ main] org.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-04-24 21:51:38.970 INFO 12068 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#5745ca0e: startup date [Tue Apr 24 21:51:38 IST 2018]; root of context hierarchy
2018-04-24 21:51:39.617 INFO 12068 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-04-24 21:51:39.999 INFO 12068 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-04-24 21:51:40.030 INFO 12068 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-04-24 21:51:40.030 INFO 12068 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-24 21:51:40.043 INFO 12068 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [I:\Program Files\Java\jdk1.8.0_66\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;I:\Program Files\Java\jdk1.8.0_66\bin;I:\Program Files\nodejs\;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Program Files (x86)\Skype\Phone\;I:\MinGW\bin;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 ^& MySQL Utilities 1.5\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 ^& MySQL Utilities 1.5\Doctrine extensions for PHP\;I:\Program Files (x86)\maven\apache-maven-3.3.9\bin\;I:\Program Files (x86)\Heroku\bin;C:\Program Files (x86)\git\cmd;I:\Program Files\Git\cmd;I:\Program Files\PostgreSQL\9.6\bin;I:\RailsInstaller\Git\cmd;I:\RailsInstaller\Ruby2.2.0\bin;I:\Program Files (x86)\Python\Scripts\;I:\Program Files (x86)\Python\;C:\Users\chirayu\AppData\Roaming\npm;C:\Users\chirayu\AppData\Local\Microsoft\WindowsApps;I:\Program Files (x86)\Microsoft VS Code\bin;;.]
2018-04-24 21:51:40.164 INFO 12068 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-04-24 21:51:40.164 INFO 12068 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1198 ms
2018-04-24 21:51:40.215 INFO 12068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-24 21:51:40.598 INFO 12068 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-04-24 21:51:40.633 INFO 12068 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-04-24 21:51:40.638 INFO 12068 --- [ main] org.demo.DemoApplication : Started DemoApplication in 2.2 seconds (JVM running for 2.797)
I am not sure what I might be missing. Please help.
I got the solution. It was due to package visibility. Main class was not able to found package in which controller class is present. So added all classes under same package. You can also place application class one level up. Got help from below link:
Spring Boot: Cannot access REST Controller on localhost (404)
Test as follow... did the trick for me remove the path from the notation #RestController then add the path #GetMapping notation as follow #GetMapping("/hello") test http://localhost:8080/hello you should get a correct response in order to have a response for this request http://localhost:8080/hello/hello need to add the follow:
#RestController
#RequestMapping(value = "/hello")
change to :
#RestController
#RequestMapping(value = "hello-controller")
public class HelloController {
#GetMapping(value="hello")
#ResponseBody
public String hello() {
return "Hello";
}
}
so you can use your api from : localhost:8080/hello-controller/hello
value in #RequestMapping below #RestController is define your class address.
value in #GetMapping is for your API address.
Hope this explanation help you.

RequestMapping of SpringBoot Not working

I am learning Spring Boot for building Applications. I am trying to build my first Spring Boot Application with a controller in different package as application. Tomcat instance comes up but request does not reach RestController registered for the URI.
Following is the controller class:
package com.spring.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloController {
#RequestMapping(value = "/abc")
public String getHi() {
System.out.println("End Point hit");
return "Hi";
}
}
Following is the application class:
package com.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(scanBasePackages = {"com.spring.controllers"})
public class SpringBootMain {
public static void main(String args[]) {
SpringApplication.run(SpringBootMain.class, args);
}
}
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>
<groupId>RandomProjects</groupId>
<artifactId>SpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>com.spring.boot.SpringBootMain</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
</dependencies>
Logs when tomcat starts:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.6.RELEASE)
2017-12-25 16:41:16.236 INFO 50314 --- [BootMain.main()] com.spring.boot.SpringBootMain : Starting SpringBootMain on f45c89be9049.ant.amazon.com with PID 50314 (/Users/sumt/Desktop/Work/RandomWork/RandomProjects/target/classes started by sumt in /Users/sumt/Desktop/Work/RandomWork/RandomProjects)
2017-12-25 16:41:16.244 INFO 50314 --- [BootMain.main()] com.spring.boot.SpringBootMain : No active profile set, falling back to default profiles: default
2017-12-25 16:41:16.408 INFO 50314 --- [BootMain.main()] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#7bbcce75: startup date [Mon Dec 25 16:41:16 IST 2017]; root of context hierarchy
2017-12-25 16:41:18.279 INFO 50314 --- [BootMain.main()] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-12-25 16:41:18.295 INFO 50314 --- [BootMain.main()] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-12-25 16:41:18.297 INFO 50314 --- [BootMain.main()] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-12-25 16:41:18.402 INFO 50314 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-12-25 16:41:18.402 INFO 50314 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2056 ms
2017-12-25 16:41:18.560 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-25 16:41:18.564 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-25 16:41:18.565 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-25 16:41:18.565 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-25 16:41:18.566 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-25 16:41:18.920 INFO 50314 --- [BootMain.main()] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#7bbcce75: startup date [Mon Dec 25 16:41:16 IST 2017]; root of context hierarchy
2017-12-25 16:41:19.005 INFO 50314 --- [BootMain.main()] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-12-25 16:41:19.007 INFO 50314 --- [BootMain.main()] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-12-25 16:41:19.041 INFO 50314 --- [BootMain.main()] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-25 16:41:19.041 INFO 50314 --- [BootMain.main()] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-25 16:41:19.084 INFO 50314 --- [BootMain.main()] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-25 16:41:19.323 INFO 50314 --- [BootMain.main()] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-12-25 16:41:19.407 INFO 50314 --- [BootMain.main()] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-12-25 16:41:19.413 INFO 50314 --- [BootMain.main()] com.spring.boot.SpringBootMain : Started SpringBootMain in 3.889 seconds (JVM running for 6.196)
I have added base package scan and even tried with #ComponentScan annotation, but the result is same when I hit URL (http://localhost:8080/abc):
There was an unexpected error (type=Not Found, status=404).
No message available
Did anyone else face the same issue? Please suggest as how did you solve this.
Your Controller should be in the Child package of SpringBootApplication.
e.g if your SpringbootApplication main method is under com.text.demo package then your Controller class should be under com.text.demo.controller.
or add
#ComponentScan("com.text.demo.controller") on Spring boot Application main class.
As people mentioned in the comment try to add your base package.
#SpringBootApplication(scanBasePackages = {"com.spring"})
Or
#SpringBootApplication
#ComponentScan("com.spring")
But why don't you use this spring initializr https://start.spring.io/ to create a spring boot project from scratch. You can also add any dependency you need.
Here is the mini controller that I tried to reproduce from your example:
SpringApplication
package com.example.demo.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan("com.example.demo.controller")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Controller
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloController {
#RequestMapping(value = "/abc")
public String getHi() {
System.out.println("End Point hit");
return "Hi";
}
}
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>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
and it works just fine!
As a suggestion, try calling default actuator endpoints e.g. http://localhost:8080/error. They come up with the Spring Boot and will show the actual status of your service.
Change your parent version number to below:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
The issue might be due to wrong import of #Service annotation. It should be a stereotype one.
Use this in your service class as usage of component scan in Springboot is not a better practice.
import org.springframework.stereotype.Service;

Embeded Mongodb to Spring Boot always recreate database

I need to embed Mongodb to Spring Boot application. But the way how I've done
it leads to re-creating mongo db every time I start app. I mean, my documents don't save to database persistently.
Every start I see in logs:
2017-11-22 12:03:49.188 INFO 10464 --- [ main] c.j.s.embedmongo.EmbeddedMongoBuilder : Initializing embedded MongoDB instance
2017-11-22 12:03:49.215 INFO 10464 --- [ main] d.f.embed.process.store.Downloader : Extract C:\Users\lev\.embedmongo\win32\mongodb-win32-x86_64-2008plus-3.2.1.zip : starting...
2017-11-22 12:03:49.483 INFO 10464 --- [ main] d.f.embed.process.store.Downloader : Extract C:\Users\lev\.embedmongo\win32\mongodb-win32-x86_64-2008plus-3.2.1.zip : extract mongodb-win32-x86_64-2008plus-3.2.1/bin/mongod.exe
2017-11-22 12:03:49.484 INFO 10464 --- [ main] d.f.embed.process.store.Downloader : Extract C:\Users\lev\.embedmongo\win32\mongodb-win32-x86_64-2008plus-3.2.1.zip : noting left
2017-11-22 12:03:49.484 INFO 10464 --- [ main] d.f.embed.process.store.Downloader : Extract C:\Users\lev\.embedmongo\win32\mongodb-win32-x86_64-2008plus-3.2.1.zip : finished
2017-11-22 12:03:49.484 INFO 10464 --- [ main] c.j.s.embedmongo.EmbeddedMongoBuilder : Starting embedded MongoDB instance
2017-11-22 12:03:51.180 INFO 10464 --- [ main] d.f.embed.process.runtime.Executable : start de.flapdoodle.embed.mongo.config.MongodConfigBuilder$ImmutableMongodConfig#867ba60
2017-11-22 12:03:51.383 INFO 10464 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:3707], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2017-11-22 12:03:51.418 INFO 10464 --- [-localhost:3707] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:1}] to localhost:3707
2017-11-22 12:03:51.419 INFO 10464 --- [-localhost:3707] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:3707, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 1]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=432755}
2017-11-22 12:03:51.509 INFO 10464 --- [ main] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:2}] to localhost:3707
2017-11-22 12:03:51.939 WARN 10464 --- [ main] o.s.d.m.c.m.BasicMongoPersistentProperty : Customizing field name for id property not allowed! Custom name will not be considered!
Probably this part leads to database overwriting:
Downloader : Extract C:\Users\lev.embedmongo\win32\mongodb-win32
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>
<groupId>guru.springframework</groupId>
<artifactId>spring-boot-mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-mongodb</name>
<description>Demo project for Spring Boot and Mongo DB</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.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-data-mongodb</artifactId>
</dependency>
<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-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
</dependency>
<dependency>
<groupId>cz.jirutka.spring</groupId>
<artifactId>embedmongo-spring</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Controller for storing document:
#RequestMapping("/check")
#ResponseBody
public List<Domain> check(){
System.out.println("/check");
Domain domain = new Domain(i++, "Check_"+String.valueOf(i), true);
dm.save(domain);
return dm.findAll();
}
Repository:
public interface DomainRepository extends MongoRepository<Domain, Long> {
Domain findFirstByDomain(String domain);
Domain findByDomainAndDisplayAds(String domain, boolean displayAds);
//Supports native JSON query string
#Query("{domain:'?0'}")
Domain findCustomByDomain(String domain);
#Query("{domain: { $regex: ?0 } })")
List<Domain> findCustomByRegExDomain(String domain);
}
appilcation.properties:
server.port = 8091
spring.data.mongodb.repositories.enabled=true
spring.data.mongodb.database=testme
How can I solve this isssue?
Because it's embedded, every time application is started the DB will be re-created as previous one has been deleted when the app was stopped.

Categories