Error when running a Spring project with MySQL support - java

I have a project and I just added MySQL support to it, along with JPA and others:
<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>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
...
I then configured the application.properties file as follows:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/spring
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
But then, this error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.web.config.SpringDataWebConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/xmlbeam/config/XMLFactoriesConfig
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.web.config.SpringDataWebConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/xmlbeam/config/XMLFactoriesConfig
I don't understand very well what's this and why it is happening. I thought Boot was supposed to take care of the main configuration? I even generated a new project with these dependencies as default but the same error persists.

The reason behind this was the Java version. I had version 14, o I uninstalled it and installed the latest LTS version, 11
sudo apt purge openjdk-14*
updated the pom.xml to use the same version, then I erased everything generated regarding of configuration and opened it again to generate them again, and everything worked.

Add following properties and try again
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57Dialect
Hope this solve your problem :)

If you have any Service and Data layer. please annotate it with #Repository and #Service annotation
Please go through mkyong java basic tutorials.
Github Link

Related

Getting error UnsupportedMediaTypeException when trying to update maven dependency to spring-webflux from spring-boot-starter-webflux

As I am trying to update spring-webflux dependency from spring-boot-starter-webflux in maven. My working code start giving error like"
From :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
changed to:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
Getting below Error:
org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json' not supported for bodyType=com.location.of.Object at org.springframework.web.reactive.function.BodyInserters.unsupportedError(BodyInserters.java:391)
Note : I am trying Jackson library for POJO and having constructor in that.
What could be issue? How to solve it?
It is because of missing Jeckson dependency which I have't included in my project's submodule. I added the dependency in child module and it worked. Thanks all.

could not auto wire activiti RuntimeService

i am learning to use activiti with spring boot
but i get some errors and searching too much about acitiviti stable version with spring boot and java
i am using java 8 and tomcat 8.0.3
i changed so many version of spring and jdk and checked so many sample and can not find any problem
i only have a controller class and spring main class
when i run the project get this error:
Error creating bean with name 'runtimeServiceBean' defined in class path resource [org/activiti/spring/boot/JpaProcessEngineAutoConfiguration$JpaConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.activiti.engine.ProcessEngine]: : Error creating bean with name 'processEngine'defined in class path resource [org/activiti/spring/boot/JpaProcessEngineAutoConfiguration$JpaConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.activiti.spring.SpringProcessEngineConfiguration]
this is my RestController and do nothing but an autowire
import org.activiti.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class MyRestController {
#Autowired
private RuntimeService runtimeService;
}
this is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<activiti.version>5.19.0.2</activiti.version>
</properties>
<dependencies>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-rest-api</artifactId>
<!--<artifactId>spring-boot-starter-rest-api</artifactId>-->
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-jpa</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
If you want to use Activiti with Spring Boot 2.x we recommend to use the new release of Activiti Core 7 which can be found here: https://search.maven.org/artifact/org.activiti/activiti-spring-boot-starter/7.0.0.SR1/jar
You can find examples here:
https://github.com/activiti/activiti-examples
after so many search i realized that spring boot 2 have problem with activiti then i change spring boot to version 1.5
and then i get new error :
'formDataResource': Unsatisfied dependency expressed through field 'formService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'formServiceBean' defined in class path resource [org/activiti/spring/boot/JpaProcessEngineAutoConfiguration$JpaConfiguration.class]: Unsatisfied dependency expressed through method 'formServiceBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'processEngine' defined in class path resource [org/activiti/spring/boot/JpaProcessEngineAutoConfiguration$JpaConfiguration.class]: Unsatisfied dependency expressed through method 'processEngine' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springProcessEngineConfiguration' defined in class path resource [org/activiti/spring/boot/JpaProcessEngineAutoConfiguration$JpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.activiti.spring.SpringProcessEngineConfiguration]
and the cause of new error : i dont have any process in src/main/resources/processes/
set some process and delete target folder
it worked for me !
With Spring Boot 2.x you have to add the dependence above, and delete the other one.`
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.0.0.SR1</version>
</dependency>`

Springboot + MySQL + Driver Class Not Found

I'm doing a simple spring boot project with a thread pool and MySQL in order to connect to MySQL whenever I'm adding spring-boot-starter-jdbc I'm getting below error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.s
Update 1:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.3</version>
</dependency>
</dependencies>
It seems like you are missing mysql-connector dependency,
Add these to your pom.
Maven:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
or gradle:
compile "mysql:mysql-connector-java:*"
Looks like you forget to add dependency to MySQL (Spring Boot is using H2 database by default), you should add next lines to your pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
First include this in your pom file.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Second, clean install the maven project.
If you are using eclipse check your JDK version and maven (Sometime jre is used instead of jdk, so, though in console everything looks fine but acutally the jar is not downloaded)
in short .m2 repo should have this jar file when maven build is done.
Scope "runtime" is good for unit test and container as tomcat etc. when container provide jdbc driver. When run standalone apps (spring-boot) you should remove it or set to "compile".

Intellij IDEA problems with connections to DB

I've imported spring-boot maven project from git. This project uses Mysql.
I've created database with customized name in workbench.
application.properties file:
spring.datasource.url = jdbc:mysql://localhost:3306/myDb?autoReconnect=true&useSSL=false
spring.datasource.username = root
spring.datasource.password= root
the same credentials i put into intellij's db-connection window.
When i click on "Test connection" i've got:
Connection to myDb#localhost failed.
[42000][1049] Unknown database 'myDb'
Pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
.....
============Updated==============
Apllication can't start with an error:
2018-04-26 13:12:22.404 ERROR 3988 --- [ost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'jwtFilterRegistration' defined in com.edsson.expopromoter.api.Application: Unsatisfied dependency expressed through method 'jwtFilterRegistration' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService' defined in file [C:\Users\koliada\Desktop\IdeaProjects\ExpoPromoter\expopromoter-api\target\classes\com\edsson\expopromoter\api\user\service\UserService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#64607e71' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#64607e71': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2018-04-26 13:12:22.428 WARN 3988 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
2018-04-26 13:12:22.478 ERROR 3988 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.edsson.expopromoter.api.user.service.UserService required a bean named 'entityManagerFactory' that could not be found.
it says
Unknown database 'myDb'
so have you checked if this database already exist?
As intellij's db-connection can not reach that database, so the problem could exist in your connect param(URL,username,password) or mysql, not the code.

Exception thrown when trying to deploy CAS maven overlay

I'm trying to setup a CAS server locally in Jboss. i'm trying to deploy the maven overlay described in this link.
https://github.com/UniconLabs/simple-cas4-overlay-template
and when i'm trying to deploy the cas.war file i got few errors and i managed to fix them by adding following dependencies.
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.spring</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>2.2.2</version>
</dependency>
Now i'm getting this error:
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.restlet.ext.spring.SpringRouter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/sun/net/httpserver/HttpHandler
I tried to find a dependency having this class but I didn't find. What am i doing wrong ?
My ultimate goal is to integrate CAS with Sprint security.
Thanks in advance.
I found the issue. It was due to not specifying "com/sun/net/httpserver/HttpHandler" under modules in jboss wildfly.
To fix the issue add the following under paths in /modules/system/layers/base/sun/jdk/main/module.xml:
<path name="com/sun/net/httpserver"/>

Categories

Resources