sorry for bad English. I get my project started with spring-boot 1.1.8, Encountered Exception No CurrentSessionContext configured, then I did some search, add property <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property> could solve this problem, but
how to config this property using java class?
update: I changed to the Hibernate4.x Way to define SessionFactory but still got the same error, please help!
Use java 1.8 and speing boot 1.1.8
here is my pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>../</m2eclipse.wtp.contextRoot>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.1.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
here is my Service and Controller
#AutoWried
SessionFactory sessionFactory;
#Service
#Transactional
public class UserServiceImpl implements UserService {
public boolean isEmailExist(String email) {
Session session = sessionFactory.openSession();
// if I use getCurrentSession(), I will got the No Session found for current thread
//Session session = sessionFactory.getCurrentSession();
Criteria c = UserCriteria.isEmailExisting(session, email);
List<UserInfo> usrs = c.list();
session.close();
if (0 == usrs.size() || usrs.isEmpty())
return false;
return true;
}
}
#Controller
public class UserRegisterController {
#Autowired
UserService userService;
#RequestMapping("/doRegist")
public void doRegister(HttpServletRequest request,
HttpServletResponse response) {
String usrEmail = request.getParameter("email");
boolean exist = userService.isEmailExist(session, usrEmail);
//todo
}
}
WebApplicatoinStarter.java
#Configuration
#EnableAutoConfiguration
#ComponentScan("com.mytest")
public class WebApplicationStarter extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(WebApplicationStarter.class);
}
public static void main(String[] args) throws Exception {
ApplicationContext context = SpringApplication.run(WebApplicationStarter.class, args);
}
#Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
return hemf.getSessionFactory();
}
}
update: here is my full stack trace:
2014-11-01 08:41:11.736 INFO 3312 --- [ main] com.hotsoft.WebApplicationStarter : Starting WebApplicationStarter on zblqmc with PID 3312 (D:\x51\p2\target\classes started by lzzafll in D:\x51\p2)
2014-11-01 08:41:11.814 INFO 3312 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#442675e1: startup date [Sat Nov 01 08:41:11 CST 2014]; root of context hierarchy
2014-11-01 08:41:13.047 INFO 3312 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2014-11-01 08:41:14.009 INFO 3312 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$dd4f2c7a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-11-01 08:41:14.041 INFO 3312 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionAttributeSource' of type [class org.springframework.transaction.annotation.AnnotationTransactionAttributeSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-11-01 08:41:14.056 INFO 3312 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionInterceptor' of type [class org.springframework.transaction.interceptor.TransactionInterceptor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-11-01 08:41:14.072 INFO 3312 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.config.internalTransactionAdvisor' of type [class org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-11-01 08:41:14.647 INFO 3312 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-11-01 08:41:14.928 INFO 3312 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2014-11-01 08:41:14.928 INFO 3312 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.55
2014-11-01 08:41:15.755 INFO 3312 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-11-01 08:41:15.771 INFO 3312 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3957 ms
2014-11-01 08:41:16.427 INFO 3312 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2014-11-01 08:41:16.427 INFO 3312 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-11-01 08:41:17.534 INFO 3312 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2014-11-01 08:41:17.550 INFO 3312 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2014-11-01 08:41:17.660 INFO 3312 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {4.3.6.Final}
2014-11-01 08:41:17.660 INFO 3312 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2014-11-01 08:41:17.660 INFO 3312 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2014-11-01 08:41:17.910 INFO 3312 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2014-11-01 08:41:18.050 INFO 3312 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2014-11-01 08:41:18.206 INFO 3312 --- [ main] o.h.h.i.ast.ASTQueryTranslatorFactory : HHH000397: Using ASTQueryTranslatorFactory
2014-11-01 08:41:18.861 INFO 3312 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-11-01 08:41:19.047 INFO 3312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.hotsoft.web.UserRegisterController.hello(java.util.Map<java.lang.String, java.lang.Object>)
2014-11-01 08:41:19.047 INFO 3312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/doRegist],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.hotsoft.web.UserRegisterController.doRegister(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2014-11-01 08:41:19.063 INFO 3312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" 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)
2014-11-01 08:41:19.063 INFO 3312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2014-11-01 08:41:19.078 INFO 3312 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-11-01 08:41:19.078 INFO 3312 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-11-01 08:41:19.413 INFO 3312 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-11-01 08:41:19.554 INFO 3312 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2014-11-01 08:41:19.569 INFO 3312 --- [ main] com.hotsoft.WebApplicationStarter : Started WebApplicationStarter in 8.551 seconds (JVM running for 9.276)
2014-11-01 08:41:32.754 INFO 3312 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2014-11-01 08:41:32.754 INFO 3312 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2014-11-01 08:41:32.786 INFO 3312 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 32 ms
2014-11-01 08:41:39.988 ERROR 3312 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No CurrentSessionContext configured!] with root cause
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1012)
at com.hotsoft.web.UserRegisterController.doRegister(UserRegisterController.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1695)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Your configuration and usage of hibernate is wrong. You are using Spring and even better Spring Boot, however what you posted tries very hard not to use those frameworks and tries to work around them. I strongly suggest using Spring Boot and let that configure the things for you.
First delete your HibernateUtils, burry it deep and never look at it again. You can also delete your AppConfig as Spring Boot can and will take care of the DataSource.
Next create a file called application.properties in your src/main/resources directory and put the following content in there.
spring.datasource.url=jdbc:mysql://localhost/mysql
spring.datasource.username=root
spring.datasource.password=
This will automatically configure a DataSource for you. You don't need the driver as that is deduced from the url you provide. Next add the following properties to configure JPA.
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
For more settings and properties I suggest a read of the Spring Boot Reference Guide for the properties check this comprehensive list.
Next in your WebApplicationStarter add the HibernateJpaSessionFactoryBean to expose the created JPA EntityManagerFactory as a SessionFactory.
#Configuration
#EnableAutoConfiguration
#ComponentScan("com.mytest")
public class WebApplicationStarter extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplicationStarter.class);
}
public static void main(String[] args) throws Exception {
ApplicationContext context = SpringApplication.run(WebApplicationStarter.class, args);
}
#Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
return hemf.getSessionFactory();
}
}
Then just #Autowire the SessionFactory into your UserServiceImpl.
#Service
#Transactional
public class UserServiceImpl implements UserService {
#Autowired
private SessionFactory sessionFactory;
}
Now you can just use the SessionFactory.
I got this error because I had
sessionFactory.getCurrentSession();
when I changed it to
sessionFactory.openSession();
it worked fine.
Related
So I've been trying to do the example from spring's official guide on connecting Spring with MySQL (https://spring.io/guides/gs/accessing-data-mysql/)
I didn't follow it 100% since I'm using STS for my overall project. The thing is when I use POSTMAN or just my browser to send data
such as :
localhost:8000/demo/add?name=First&email=someemail#someemailprovider.com
I get the error not found.
The java code is the same as in the guide . My application.properties is this:
server.port=8000
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
and my pom.xml is this
<?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>dbDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dbDemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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-jpa</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.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>
The answer from my browser is:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing
this as a fallback.
Tue Jul 10 21:15:52 EEST 2018
There was an unexpected error (type=Not Found, status=404).
No message available
And from postman :
{
"timestamp": "2018-07-10T18:08:28.208+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/demo/add"
}
The log from spring's console is :
:: Spring Boot :: (v2.0.3.RELEASE)
2018-07-10 20:32:36.453 INFO 7815 --- [ main] com.example.demo.DbDemoApplication : Starting DbDemoApplication on max-kans with PID 7815 (/home/max/Desktop/Fetina/Earino/projectTL/softEngine/dbDemo/target/classes started by max in /home/max/Desktop/Fetina/Earino/projectTL/softEngine/dbDemo)
2018-07-10 20:32:36.496 INFO 7815 --- [ main] com.example.demo.DbDemoApplication : No active profile set, falling back to default profiles: default
2018-07-10 20:32:36.742 INFO 7815 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#2aa5fe93: startup date [Tue Jul 10 20:32:36 EEST 2018]; root of context hierarchy
2018-07-10 20:32:41.650 INFO 7815 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$5ab04215] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-07-10 20:32:42.775 INFO 7815 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8000 (http)
2018-07-10 20:32:43.344 INFO 7815 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-07-10 20:32:43.344 INFO 7815 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-07-10 20:32:44.326 INFO 7815 --- [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: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-07-10 20:32:47.580 INFO 7815 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-07-10 20:32:47.581 INFO 7815 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 10868 ms
2018-07-10 20:32:48.187 INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-07-10 20:32:48.192 INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-10 20:32:48.193 INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-10 20:32:48.193 INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-10 20:32:48.193 INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-10 20:32:50.090 INFO 7815 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-07-10 20:32:54.757 INFO 7815 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2018-07-10 20:32:55.307 INFO 7815 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-07-10 20:32:55.640 INFO 7815 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2018-07-10 20:32:58.848 INFO 7815 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.17.Final}
2018-07-10 20:32:59.125 INFO 7815 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2018-07-10 20:33:00.513 INFO 7815 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-07-10 20:33:02.175 INFO 7815 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-07-10 20:33:03.329 INFO 7815 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl#7a8406c2'
2018-07-10 20:33:03.331 INFO 7815 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-07-10 20:33:06.623 INFO 7815 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-10 20:33:08.065 INFO 7815 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for #ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#2aa5fe93: startup date [Tue Jul 10 20:32:36 EEST 2018]; root of context hierarchy
2018-07-10 20:33:08.206 WARN 7815 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2018-07-10 20:33:08.322 INFO 7815 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-10 20:33:08.324 INFO 7815 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-07-10 20:33:08.368 INFO 7815 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-10 20:33:08.368 INFO 7815 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-10 20:33:09.610 INFO 7815 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-10 20:33:09.613 INFO 7815 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-07-10 20:33:09.622 INFO 7815 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-07-10 20:33:10.003 INFO 7815 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8000 (http) with context path ''
2018-07-10 20:33:10.154 INFO 7815 --- [ main] com.example.demo.DbDemoApplication : Started DbDemoApplication in 35.885 seconds (JVM running for 39.933)
2018-07-10 20:34:07.354 INFO 7815 --- [nio-8000-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-07-10 20:34:07.354 INFO 7815 --- [nio-8000-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-07-10 20:34:07.962 INFO 7815 --- [nio-8000-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 608 ms
My controller is pretty much identical to the one from spring's guide :
package hello;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import hello.User;
import hello.UserRepository;
#Controller // This means that this class is a Controller
#RequestMapping(path="/demo")
// This means URL's start with /demo (after Application path)
public class UserController {
#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;
#GetMapping(path="/add") // Map ONLY GET Requests
public #ResponseBody String addNewUser(#RequestParam String name,
#RequestParam String email) {
// #ResponseBody means the returned String is the response,
// not a view name
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();
}
}
Your #SpringBootApplication class is in the package com.example.demo (according to the startup log), but your #Controller is in package hello. By default, Spring Boot won't auto-discover Spring classes that are outside the package the #SpringBootApplication class is in. Either move the UserController somewhere under the com.example.demo, or add #ComponentScan annotation targeting your hello package to the main Spring Boot app class.
I have a problem with my new Project , I receive this error that I can not solve, I loaded the project on github I think it's a configuration problem.
https://github.com/DeAngelisLuca/Traccialo
application.properties
spring.datasource.url= jdbc:postgresql://localhost/traccialo
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.hibernate.hbm2ddl.auto=update
spring.jpa.hibernate.show_sql=true
hibernate.temp.use_jdbc_metadata_defaults false
part of stackTrace
2018-04-15 19:54:27.672 INFO 9696 --- [ main] i.d.Traccialo.ServletInitializer : Starting ServletInitializer v0.0.1-SNAPSHOT on PC-Luca with PID 9696 (C:\Users\Luca\eclipse-workspace-professional\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Traccialo\WEB-INF\classes started by Luca in C:\Users\Luca\Downloads\eclipse-jee-oxygen-3-win32-x86_64\eclipse)
2018-04-15 19:54:27.675 INFO 9696 --- [ main] i.d.Traccialo.ServletInitializer : No active profile set, falling back to default profiles: default
2018-04-15 19:54:27.752 INFO 9696 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#25b78608: startup date [Sun Apr 15 19:54:27 CEST 2018]; root of context hierarchy
2018-04-15 19:54:28.569 INFO 9696 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Tomcat; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]]
2018-04-15 19:54:29.273 INFO 9696 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$dc6cce97] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-04-15 19:54:29.358 INFO 9696 --- [ main] o.a.c.c.C.[.[localhost].[/Traccialo] : Initializing Spring embedded WebApplicationContext
2018-04-15 19:54:29.358 INFO 9696 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1606 ms
2018-04-15 19:54:29.768 INFO 9696 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-15 19:54:29.769 INFO 9696 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'errorPageFilter' to: [/*]
2018-04-15 19:54:29.769 INFO 9696 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-04-15 19:54:29.770 INFO 9696 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-04-15 19:54:29.770 INFO 9696 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-04-15 19:54:29.770 INFO 9696 --- [ main] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2018-04-15 19:54:29.770 INFO 9696 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-04-15 19:54:30.022 INFO 9696 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-04-15 19:54:30.153 INFO 9696 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2018-04-15 19:54:30.229 INFO 9696 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-04-15 19:54:30.264 INFO 9696 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2018-04-15 19:54:30.465 INFO 9696 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.16.Final}
2018-04-15 19:54:30.469 INFO 9696 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2018-04-15 19:54:30.562 INFO 9696 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-04-15 19:54:30.836 INFO 9696 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
2018-04-15 19:54:30.961 INFO 9696 --- [ main] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_161]
stacktrace photo
This exception only came when there is a mismatch of jdbc driver 4 (I think so, I have faced earlier this), As you know hibernate is a ORM tool so to create a connection it needs some meta-data information from the database.
You can disable this feature just by putting this line in application.properties
hibernate.temp.use_jdbc_metadata_defaults = false
if you have hibernate.cfg.xml as a configuration file then you may add this line to your config file
hibernate.cfg.xml file add below property
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
Note :- If you see your application.properties key-value pair must be seperated with = (assignment operator). please add this to your code
Hope this will help you out. Thanks :)
I am trying to build a webapplication using the spring boot initiliazer project option in netbeans 8.2. When I start it up the RUN progress bar hangs after completing the initialization of the dispatcher Servlet. When I try to get a page from the localhost:8080 I get an error returned. I think it has something to do with the dispatcher servlet configuration but i'm not sure, could someone help me please? Below is the output log.
cd C:\Users\maurice\Documents\NetBeansProjects\seedcalendarwebinit; SPRING_OUTPUT_ANSI_ENABLED=always "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_131" cmd /c "\"\"C:\\Program Files\\NetBeans 8.2\\java\\maven\\bin\\mvn.bat\" -Drun.jvmArguments=\"-noverify -XX:TieredStopAtLevel=1\" -Drun.mainClass=com.example.demo.SeedcalendarApplication -Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans 8.2\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 spring-boot:run\""
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...
------------------------------------------------------------------------
Building seedcalendar 0.0.1-SNAPSHOT
------------------------------------------------------------------------
>>> spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) # demo >>>
--- maven-resources-plugin:2.6:resources (default-resources) # demo ---
Using 'UTF-8' encoding to copy filtered resources.
Copying 1 resource
Copying 3 resources
--- maven-compiler-plugin:3.1:compile (default-compile) # demo ---
Nothing to compile - all classes are up to date
--- maven-resources-plugin:2.6:testResources (default-testResources) # demo ---
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory C:\Users\maurice\Documents\NetBeansProjects\seedcalendarwebinit\src\test\resources
--- maven-compiler-plugin:3.1:testCompile (default-testCompile) # demo ---
Nothing to compile - all classes are up to date
<<< spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) # demo <<<
--- spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) # demo ---
Attaching agents: []
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.6.RELEASE)
2017-08-01 20:31:48.213 INFO 2796 --- [ main] c.example.demo.SeedcalendarApplication : Starting SeedcalendarApplication on DESKTOP-70S441T with PID 2796 (C:\Users\maurice\Documents\NetBeansProjects\seedcalendarwebinit\target\classes started by maurice in C:\Users\maurice\Documents\NetBeansProjects\seedcalendarwebinit)
2017-08-01 20:31:48.215 INFO 2796 --- [ main] c.example.demo.SeedcalendarApplication : No active profile set, falling back to default profiles: default
2017-08-01 20:31:48.412 INFO 2796 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#4fcd19b3: startup date [Tue Aug 01 20:31:48 CEST 2017]; root of context hierarchy
2017-08-01 20:31:48.879 INFO 2796 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Tomcat; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]]
2017-08-01 20:31:49.675 INFO 2796 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-01 20:31:49.685 INFO 2796 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-08-01 20:31:49.687 INFO 2796 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-08-01 20:31:49.764 INFO 2796 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-08-01 20:31:49.765 INFO 2796 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1357 ms
2017-08-01 20:31:49.877 INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-08-01 20:31:49.881 INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-08-01 20:31:49.882 INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-08-01 20:31:49.882 INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-08-01 20:31:49.883 INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-08-01 20:31:50.319 INFO 2796 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-08-01 20:31:50.329 INFO 2796 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-08-01 20:31:50.378 INFO 2796 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
2017-08-01 20:31:50.379 INFO 2796 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-08-01 20:31:50.380 INFO 2796 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-08-01 20:31:50.410 INFO 2796 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-08-01 20:31:50.499 INFO 2796 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-08-01 20:31:50.756 INFO 2796 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-08-01 20:31:51.074 INFO 2796 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#4fcd19b3: startup date [Tue Aug 01 20:31:48 CEST 2017]; root of context hierarchy
2017-08-01 20:31:51.290 INFO 2796 --- [ 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-08-01 20:31:51.291 INFO 2796 --- [ 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-08-01 20:31:51.317 INFO 2796 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-01 20:31:51.317 INFO 2796 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-01 20:31:51.348 INFO 2796 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-01 20:31:51.745 INFO 2796 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-08-01 20:31:51.791 INFO 2796 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-01 20:31:51.795 INFO 2796 --- [ main] c.example.demo.SeedcalendarApplication : Started SeedcalendarApplication in 3.8 seconds (JVM running for 4.118)
2017-08-01 20:32:08.031 INFO 2796 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-08-01 20:32:08.031 INFO 2796 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-08-01 20:32:08.047 INFO 2796 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
EDIT: I've rerun only this time in DEBUG LOG mode, this is the extra information I get from the dispatcher servlet
2017-08-01 21:07:28.999 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing servlet 'dispatcherServlet'
2017-08-01 21:07:28.999 INFO 13376 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-08-01 21:07:29.000 INFO 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-08-01 21:07:29.000 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Using MultipartResolver [org.springframework.web.multipart.support.StandardServletMultipartResolver#adcfad9]
2017-08-01 21:07:29.004 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver#58fd7fbe]
2017-08-01 21:07:29.008 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver#f0ee540]
2017-08-01 21:07:29.013 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator#2dae4a0b]
2017-08-01 21:07:29.019 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager#d8475b6]
2017-08-01 21:07:29.019 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Published WebApplicationContext of servlet 'dispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet]
2017-08-01 21:07:29.019 INFO 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
2017-08-01 21:07:29.019 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Servlet 'dispatcherServlet' configured successfully
2017-08-01 21:07:29.033 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/]
2017-08-01 21:07:29.036 DEBUG 13376 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /
2017-08-01 21:07:29.040 DEBUG 13376 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/]
2017-08-01 21:07:29.040 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/] are [/**]
2017-08-01 21:07:29.041 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/] are {}
2017-08-01 21:07:29.042 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#163042ea]]] and 1 interceptor
2017-08-01 21:07:29.043 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/] is: -1
2017-08-01 21:07:29.044 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-08-01 21:07:29.044 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-08-01 21:07:29.053 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2017-08-01 21:07:29.053 DEBUG 13376 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-08-01 21:07:29.057 DEBUG 13376 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2017-08-01 21:07:29.057 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2017-08-01 21:07:29.118 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2017-08-01 21:07:29.128 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#3b27b497] based on requested media type 'text/html'
2017-08-01 21:07:29.128 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#3b27b497] in DispatcherServlet with name 'dispatcherServlet'
2017-08-01 21:07:29.142 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
my dispatcher servlet initializer class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
*
* #author maurice
*/
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
#Override
protected String[] getServletMappings(){
return new String[]{"/"};
}
#Override
protected Class<?>[] getRootConfigClasses(){
return new Class<?>[] {RootConfig.class};
}
#Override
protected Class<?>[] getServletConfigClasses(){
return new Class<?>[] {WebConfig.class};
}
}
my webconfig
#Configuration
#EnableWebMvc
#ComponentScan("Controllers")
public class WebConfig extends WebMvcConfigurerAdapter{
#Bean
public ViewResolver viewResolver(
SpringTemplateEngine templateEngine){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine);
return viewResolver;
}
#Bean
public TemplateEngine templateEngine(
TemplateResolver templateResolver){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
#Bean
public TemplateResolver templateResolver(){
TemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}
//zorgt ervoor dat statische requests niet meer door de servletdispatcher afgehandeld worden
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
configurer.enable();
}
}
my controller
#Controller
#RequestMapping(value = {"","/"})
public class LoginScreenController
{
public LoginScreenController() {
}
public LoginScreenController(User_ user) {
this.user = user;
}
#RequestMapping(value = "/hello", method = GET)
public String loginForm(){
return "login";
}
}
this is my POM
<?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.seedcalendar</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>seedcalendar</name>
<description>seedcalendar</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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>mysql</groupId>
<artifactId>mysql-connector-java</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>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
this is the error I see in the browser
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Aug 01 20:32:31 CEST 2017
There was an unexpected error (type=Not Found, status=404).
No message available
thank you
EDIT:
So I've added this to my main class in order to check if the controller gets included in the application context.
#SpringBootApplication
public class SeedcalendarApplication implements ApplicationContextAware {
private static ApplicationContext ac;
public static void main(String[] args) {
SpringApplication.run(SeedcalendarApplication.class, args);
System.out.println("---------------"+ac.containsBean("loginScreenController"));
LoginScreenController log = ac.getBean(LoginScreenController.class);
//for (String i : ac.getBeanDefinitionNames()) System.out.println(i);
}
#Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.ac = applicationContext;
}
}
It outputs FALSE, meaning the logincontroller isent present in the application context, this explains why a get method cannot be found. The next step I took was include the logincontroller as a bean to both Rootconfig and Webconfig classes like so:
#Bean
public LoginScreenController loginScreenController(){
return new LoginScreenController();
}
but i still get a org.springframework.beans.factory.NoSuchBeanDefinitionException when I try to use the getBean() method in the main class! does anyone know why?
the cause of the problem was that the main class and the config classes were in separate packages. Very strange... but problem solved.
I'm using spring boot 1.5.4, i'm following a tutorial where it shows that just by adding h2-console to the localhost:8080/ url you can access the console. But when i do that i have a 404 Whitelabel error.
These are the dependencies
<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-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--WebJars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.1</version>
</dependency>
<!--jpa and database-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
And this is the spring boot console log
2017-07-19 01:35:25.222 INFO 8644 --- [ main] c.e.s.SpringBootTest00Application : Starting SpringBootTest00Application on DESKTOP-K8Q0B2R with PID 8644 (started by Talon in C:\Users\Talon\Desktop\java netbeans\01\springBootTest00)
2017-07-19 01:35:25.225 INFO 8644 --- [ main] c.e.s.SpringBootTest00Application : No active profile set, falling back to default profiles: default
2017-07-19 01:35:25.544 INFO 8644 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#37574691: startup date [Wed Jul 19 01:35:25 CEST 2017]; root of context hierarchy
2017-07-19 01:35:27.236 INFO 8644 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-19 01:35:27.248 INFO 8644 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-07-19 01:35:27.249 INFO 8644 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-07-19 01:35:27.378 INFO 8644 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-19 01:35:27.378 INFO 8644 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1838 ms
2017-07-19 01:35:27.578 INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-19 01:35:27.582 INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-19 01:35:27.584 INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-19 01:35:27.584 INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-19 01:35:27.584 INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-19 01:35:28.020 INFO 8644 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-07-19 01:35:28.037 INFO 8644 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-07-19 01:35:28.122 INFO 8644 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
2017-07-19 01:35:28.123 INFO 8644 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-07-19 01:35:28.180 INFO 8644 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-07-19 01:35:28.219 INFO 8644 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-07-19 01:35:28.321 INFO 8644 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2017-07-19 01:35:28.742 INFO 8644 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2017-07-19 01:35:28.757 INFO 8644 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
2017-07-19 01:35:28.809 INFO 8644 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-07-19 01:35:29.119 INFO 8644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#37574691: startup date [Wed Jul 19 01:35:25 CEST 2017]; root of context hierarchy
2017-07-19 01:35:29.203 INFO 8644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.example.springBootTest00.controllers.IndexController.index()
2017-07-19 01:35:29.205 INFO 8644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/product],methods=[POST]}" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.saveOrUpdateProduct(com.example.springBootTest00.domain.Product)
2017-07-19 01:35:29.206 INFO 8644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/product/edit/{id}]}" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.edit(java.lang.Integer,org.springframework.ui.Model)
2017-07-19 01:35:29.206 INFO 8644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/product/new]}" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.newProduct(org.springframework.ui.Model)
2017-07-19 01:35:29.206 INFO 8644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/product/delete/{id}]}" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.deleteProduct(java.lang.Integer,org.springframework.ui.Model)
2017-07-19 01:35:29.206 INFO 8644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/products]}" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.listProducts(org.springframework.ui.Model)
2017-07-19 01:35:29.206 INFO 8644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/product/{id}]}" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.getProduct(java.lang.Integer,org.springframework.ui.Model)
2017-07-19 01:35:29.209 INFO 8644 --- [ 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-07-19 01:35:29.209 INFO 8644 --- [ 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-07-19 01:35:29.243 INFO 8644 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-19 01:35:29.244 INFO 8644 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-19 01:35:29.284 INFO 8644 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-19 01:35:29.920 INFO 8644 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-07-19 01:35:29.983 INFO 8644 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-19 01:35:29.987 INFO 8644 --- [ main] c.e.s.SpringBootTest00Application : Started SpringBootTest00Application in 5.052 seconds (JVM running for 5.401)
I have some classes with #Service / #Controller working properly and all the requests are mapped to url strings other than h2-console. Should i import something else in the pom or configure something in application.properties ?
Writing in simple Steps:
In Application.properties file include
spring.h2.console.path=/h2
spring.h2.console.enabled=true
And in pom.xml Include devtools dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
re-package and open http://localhost:[port]/h2 you are all set
other properties you have to include is
spring.datasource.url=jdbc:h2:file:~/h2db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
the file h2db will store in userprofile folder of windows
for example : C:\Users\[profile]
I am trying to run Spring boot application but I am facing a problem. Help me please. Why do I have this error?
pom.xml
<groupId>com.bwebmedia.laughtercounter</groupId>
<artifactId>laughtercounter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>laughtercounter</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/>
</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.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</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.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.0.0</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
`AppConfig
#Configuration
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
#ComponentScan
#EnableAsync
#EnableCaching
#EnableScheduling
public class AppConfig {
}
Application
#EnableDiscoveryClient
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties
server.port=8888
spring.main.banner-mode=off
#database config
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url= jdbc:postgresql://localhost:5432/laughterDB
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create-drop
#rabbit config
rabbit.host=localhost
rabbit.port=5672
rabbit.username=guest
rabbit.password=guest
#60 sec
spring.datasource.hikari.connection-timeout=60000
# max 5
spring.datasource.hikari.maximum-pool-size=5
Controller
#RestController
public class ClientController {
final private ClientService clientService;
#Autowired
public ClientController(ClientService clientService) {
this.clientService = clientService;
}
#RequestMapping(value = "/create", method = RequestMethod.POST)
public void create(#RequestBody List<Client> client,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new InvalidRequestException("Not correct request data");
}
clientService.create(client);
}
}
Model
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class Client {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private int seat;
private String gender;
private int cameraId;
private int height;
private int topY;
private int width;
private int leftX;
private long smile;
}
Repository
#Repository
public interface ClientRepository extends JpaRepository<Client, Long> {
}
Service
#Service
#Slf4j
public class ClientServiceImpl implements ClientService {
private ClientRepository clientRepository;
#Autowired
public ClientServiceImpl(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
#Override
public void create(List<Client> client) {
client.forEach(x -> clientRepository.save(x));
}
}
Error output
Connected to the target VM, address: '127.0.0.1:37019', transport: 'socket'
2017-03-02 17:47:09.286 INFO 8561 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#294e5088: startup date [Thu Mar 02 17:47:09 EET 2017]; root of context hierarchy
2017-03-02 17:47:10.066 INFO 8561 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-03-02 17:47:10.188 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$332fe413] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:11.174 INFO 8561 --- [ main] c.bwebmedia.laughtercounter.Application : No active profile set, falling back to default profiles: default
2017-03-02 17:47:11.212 INFO 8561 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#3aefae67: startup date [Thu Mar 02 17:47:11 EET 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext#294e5088
2017-03-02 17:47:14.472 INFO 8561 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Tomcat; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]]
2017-03-02 17:47:14.546 WARN 8561 --- [ main] o.s.c.a.ConfigurationClassPostProcessor : Cannot enhance #Configuration bean definition 'refreshScope' since its singleton instance has been created too early. The typical cause is a non-static #Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2017-03-02 17:47:14.947 INFO 8561 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=ffa7c2d3-1b2b-3c4f-b2a5-e20a48541c23
2017-03-02 17:47:15.024 INFO 8561 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-03-02 17:47:15.293 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [class org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$dd819f44] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:15.390 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.retry.annotation.RetryConfiguration' of type [class org.springframework.retry.annotation.RetryConfiguration$$EnhancerBySpringCGLIB$$cd9793b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:15.462 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$1715e116] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:15.574 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cache.annotation.ProxyCachingConfiguration' of type [class org.springframework.cache.annotation.ProxyCachingConfiguration$$EnhancerBySpringCGLIB$$60281734] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:15.649 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration' of type [class org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration$$EnhancerBySpringCGLIB$$549bdbd8] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:15.740 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.cache-org.springframework.boot.autoconfigure.cache.CacheProperties' of type [class org.springframework.boot.autoconfigure.cache.CacheProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:15.764 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers' of type [class org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:15.775 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration' of type [class org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration$$EnhancerBySpringCGLIB$$f6374eb9] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:15.833 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'cacheManager' of type [class org.springframework.cache.concurrent.ConcurrentMapCacheManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:15.835 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'cacheAutoConfigurationValidator' of type [class org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration$CacheManagerValidator] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:16.048 INFO 8561 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$332fe413] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-02 17:47:17.019 INFO 8561 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8888 (http)
2017-03-02 17:47:17.044 INFO 8561 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-03-02 17:47:17.047 INFO 8561 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5
2017-03-02 17:47:17.303 INFO 8561 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-03-02 17:47:17.304 INFO 8561 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 6092 ms
2017-03-02 17:47:17.878 INFO 8561 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-03-02 17:47:17.887 INFO 8561 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-03-02 17:47:17.888 INFO 8561 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-03-02 17:47:17.889 INFO 8561 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-03-02 17:47:17.890 INFO 8561 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-03-02 17:47:18.914 INFO 8561 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-03-02 17:47:18.961 INFO 8561 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-03-02 17:47:19.407 INFO 8561 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.11.Final}
2017-03-02 17:47:19.413 INFO 8561 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-03-02 17:47:19.417 INFO 8561 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-03-02 17:47:19.646 INFO 8561 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-03-02 17:47:20.841 INFO 8561 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL94Dialect
2017-03-02 17:47:21.174 INFO 8561 --- [ main] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2017-03-02 17:47:21.179 INFO 8561 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType#3b4f1eb
2017-03-02 17:47:21.569 INFO 8561 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2017-03-02 17:47:21.572 INFO 8561 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
2017-03-02 17:47:21.633 INFO 8561 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-03-02 17:47:21.731 WARN 8561 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientController' defined in file [/home/spanky/IdeaProjects/laughter-counter-server/target/classes/com/bwebmedia/laughtercounter/controller/ClientController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientServiceImpl' defined in file [/home/spanky/IdeaProjects/laughter-counter-server/target/classes/com/bwebmedia/laughtercounter/service/impl/ClientServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.bwebmedia.laughtercounter.repository.ClientRepository]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2017-03-02 17:47:21.732 INFO 8561 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-03-02 17:47:21.732 INFO 8561 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2017-03-02 17:47:21.733 INFO 8561 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
2017-03-02 17:47:21.745 INFO 8561 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat
2017-03-02 17:47:21.797 INFO 8561 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-03-02 17:47:22.103 ERROR 8561 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.bwebmedia.laughtercounter.service.impl.ClientServiceImpl required a bean of type 'com.bwebmedia.laughtercounter.repository.ClientRepository' that could not be found.
Action:
Consider defining a bean of type 'com.bwebmedia.laughtercounter.repository.ClientRepository' in your configuration.
Disconnected from the target VM, address: '127.0.0.1:37019', transport: 'socket'
Process finished with exit code 1
Error in not #Autowired repository. Not find models and other parts. I'm use Linux(Ubuntu dist). Sometimes write error with context. Why I am facing this error?
Do not exclude DataSourceAutoConfiguration.class in your AppConfig class.