h2database login screen not working in my spring application - spring

I am using h2dabase as in memory database in my application.
I have added following dependency in my pom.xml file.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
Following is my application.properties
spring.application.name=currency-exchange-service
server.port=8000
spring.jpa.show-sql=true
spring.h2.console.enabled=true
Following is the error I am getting on the browser:

To reproduce the error, I've downloaded a fresh Spring boot project with the spring-boot-starter-web and h2 modules. When using your settings, the URL was inaccessible just like your screenshot showed.
The URL did became accessible when I added
spring.h2.console.path=/h2-console to application.properties.

Related

Active profile ignored

Sample code
I have already added "spring.profiles.active=local" in application.properties file
But while running the application it is not able to recognize the profile and running with default profile
application.properties
# Tomcat Server Config
server.port=8071
#Profile Config
spring.profiles.active=local
application-local.properties
# Spring JPA Config
spring.datasource.url=jdbc:postgresql://localhost:5432/User
spring.datasource.username=postgres
spring.datasource.password=amt123`
# Hibernate Config
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=none
"spring.devtools.add-properties" default value is true
Once it is set to false, spring boot disables profile configuration.
For me even if I set the value to true, still profile configuration was not enabled
I fixed this issue by removing the dev-tool dependency and adding it again.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
I am not sure on the internal workflow.
If anyone has a proper explanation for this, please comment

Unable to serve JSP in Spring Boot applications

I have tried several tutorials to serve JSP pages using Spring Boot. They all return a 404 page not found error.
To overcome the known limitations, I'm using a WAR packaging, with the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
I have defined the path where JSP pages are in application.properties:
spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp
When requesting a JSP page, the following WARN is displayed:
WARN 10251 --- [io-8080-exec-11] o.s.w.s.r.ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/hello.jsp]
Have JSP been deprecated in Spring Boot 2? Do you have any Spring Boot 2 working example with JSP ?
can you please try adding scope in your dependency just like this
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
I'm using Intellij IDEA. I found out that we cannot just run the SpringBootApplication main class directly. We need to let the maven do the work.
Short solution: maven run you application by the command below from your module root directory
mvn clean package spring-boot:run
You can also add a run configuration using Maven so that you don't have to type the command every time.
Some said JSPs folder should be put under src/main/resources/META-INF/resources/WEB-INF/jsp. This indeed solves the spring boot application run problem though, it will fail when you run the application using tomcat. So we still need to keep the structure if we are going to deploy the application to Tomcat.
Unfortunately, I couldn't find any Spring Boot 2 example able to serve JSP pages as they all return a 404 error. As a workaround I have configured the application to be deployed on WildFly, as described in this tutorial and run my application with JSP on WildFly.
If you want example here it is.
This also help you.

Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource Spring Boot

I am getting following error when I try to run spring boot application.
Description:
Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:
Property: driverclassname
Value: oracle.jdbc.OracleDriver
Origin: "driverClassName" from property source "source"
Reason: Unable to set value for property driver-class-name
Action:
Update your application's configuration
This is same issue I have but i am not using maven.
I am using spring Boot 2.0.0 with following starters.
dependencies {
compile "org.springframework.boot:spring-boot-starter-web"
compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1"
testCompile "org.springframework.boot:spring-boot-starter-test"
}
And this is my application.properties file
spring.datasource.url= *****
spring.datasource.username= ******
spring.datasource.password= ******
Same problem with me (Spring boot 2),
I Fixed add driver-class.
Look up application.properties file.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Full code.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=upate
spring.datasource.url=jdbc:mysql://localhost:3306/database_name
spring.datasource.username=admin
spring.datasource.password=admin1234
As Stephane Nicoll said, you don't have driver on your classpath. You need to include jdbc driver on your gradle build as below. However, you don't have to stick to driver version that I have included.
dependencies {
compile "org.springframework.boot:spring-boot-starter-web"
compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1"
testCompile "org.springframework.boot:spring-boot-starter-test"
runtime('com.oracle:ojdbc7:12.1.0.2.0')
}
I had the same error when updating from Spring Boot 2.0.6 to Spring Boot 2.1.6.
Explicitly setting driver class name spring.datasource.driver-class-name=com.mysql.jdbc.Driver in application.properties has resolved the issue
I have added the below in properties file
spring.datasource.driverclassname = com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
and added the below in POM file
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
It is working fine now.
You have to add
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
dependency in your pom.xml file
The driver is not on your classpath, this is an interesting problem and I think the failure analyzer can be improved to avoid that misleading message. If that's your problem, please confirm and open an issue so that we try to improve it.
We have to add the dependency and have to remove property "spring.datasource.driver-class-name"
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
In case anyone is running intelliJ this error isn't immediately clear it can be caused by a missing profile. Eg. missing vm args for -Dspring.profiles.active=local (or whatever your property file name might be)
You just need to add below in pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
If youre running intelliJ and run into this issue just add this dependency in your pom.xml file:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
This happened to me as well.
But after adding the correct MySQL version in my pom.xml, and explicitly adding the driver details to application.properties resolved the issue.
Application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
Add the MySQL version to pom.xml as per the version installed.
Check your pom.xml if you dont have the mysql dependency, can add like this
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Befor of update the file, run maven
mvn clean install
to update your maven's dependencies
I had the same error, the mistake was in pom.xml file. I had a mistake in my SQL connector dependency. If its okay in your case check also application.properties file you may not include driver
spring.datasource.url=jdbc:mysql://localhost:3307/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp
All of these answers require code changes, and in my situation the code wasn't the problem. The error would occur randomly in my development environment, seamingly for no reason, not associated with any config or code change that I'd made.
I think I've found a consistent way to fix it, but I'm not sure why it works.
Shut down InteliJ IDEA
Delete ".idea" and "build" folders
Restart IDEA
This seems to resolve it, but the random nature of this really bothers me. I don't know why it occurs, or why the above fixes it.
I'm just adding this in case it helps others.
I only needed to re-import all gradle dependencies.
Add the maven dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Add or modify the application.properties file.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
Older version used:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
This does NOT work anymore; hence, the change to spring.jpa.properties.hibernate.dialect.
Resulting in the following:
spring.datasource.url=jdbc:mysql://localhost:3306/<db name>
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=<db username>
spring.datasource.password=<db password>
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.database=mysql
spring.jpa.show-sql=true
Clean the build artifacts from previous builds
mvn clean install
An for IntelliJ, invalidate the cache and restart.
File --> Invalidate Caches...
Run the project.

intelij spring boot hot swap causes context reload

I am using Intellij 2017.2 with Spring Boot 1.5.4
When I recompile my current class with ctr+shift+F9, instead of the IDE doing a bytecode hotswap, the spring container gets reloaded.
On top of that, after the reload my RestConroller no longer works
I have tried adding / removing from my pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
please advise
I am not sure if this is a spring boot bug as of the new version, but I ended up disabling manually the hot reload via properties:
This is done by setting the following in your application.properties:
spring.devtools.restart.enabled=false
The official doc:
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart-disable

Spring-boot populate H2 database with schema.sql and data.sql

I set up Spring-boot to work with H2 in-memory database
application.properties file is in the /config directory
and it looks like, this file is processed
spring.datasource.url=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.path=/myconsole
spring.h2.console.enabled=true
spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=data.sql
This file is processed and the console appears at /myconsole
But the schema.sql and data.sql are not processed and db is empty.
I placed schema.sql and data.sql files both under /config and /src/main/resources.
SQL language instructions are correct and I can populate the table using console input.
Another strange thing is
even though i name db as
spring.datasource.url=jdbc:h2:mem:mydb
the spring console loads another database testdb
o.s.j.d.e.EmbeddedDatabaseFactory --- Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
How to load the H2 database correctly?
Resolved the issue.
The spring boot app requires its ownd jdbc dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
NON-boot dependency, that I had, is NOT enough alone:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
Without the "spring-boot-starter-jdbc" dependency
"spring.datasource.url" settings in file "application.properties"
are not processed.
That file is actually processed, but not the jdbc settings.
Spring boot will create its own testdb in the memory, which destroys the data
after closing the application.

Resources