Home>
When you run App.java
java.lang.NoClassDefFoundError: javax/validation/ValidationException
(abridgement)
at com.example.App.main (App.java:34)
Caused by: java.lang.ClassNotFoundException: javax.validation.ValidationException
java.lang.IllegalStateException: Failed to execute CommandLineRunner
(abridgement)
at com.example.App.main (App.java:34)
Caused by: org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction;nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException: null
Error message is output to the console and it does not work properly.
I would like to know the solution.
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import com.example.domain.Customer;
import com.example.repository.CustomerRepository;
import com.example.service.CustomerService;
@EnableAutoConfiguration
@ComponentScan
public class App implements CommandLineRunner {
@Autowired
CustomerService customerService;
@Autowired
NamedParameterJdbcTemplate jdbcTemplate;
@Autowired
CustomerRepository customerRepository;
@Override
public void run (String ... strings) throws Exception {
Customer created = customerRepository.save (new Customer (null, "Hidetoshi", "Dekisugi"));
System.out.print (created + "is created!");
customerRepository.findAll (). forEach (System.out :: println);
}
public static void main (String [] args) {
SpringApplication.run (App.class, args);
}
}
AppConfig.java
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import net.sf.log4jdbc.Log4jdbcProxyDataSource;
import javax.sql.DataSource;@Configuration
public class AppConfig {
@Autowired
DataSourceProperties datasourceProperties;
DataSource datasource;
@ConfigurationProperties (prefix = "spring.datasource")
@Bean (destroyMethod = "close")
DataSource realDataSource () {
@SuppressWarnings ("rawtypes")
DataSourceBuilder factory = DataSourceBuilder factory
.create (this.datasourceProperties.getClassLoader ())
.url (this.datasourceProperties.getUrl ())
.username (this.datasourceProperties.getUsername ())
.password (this.datasourceProperties.getPassword ());
this.datasource = factory.build ();
return this.datasource;
}
@Primary
@Bean
Log4jdbcProxyDataSource datasource () {
return new Log4jdbcProxyDataSource (this.datasource);
}
}
CustomorRepository.java
package com.example.repository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.example.domain.Customer;
@Repository
@Transactional
public class CustomerRepository {
@Autowired
NamedParameterJdbcTemplate jdbcTemplate;
private static final RowMapper<Customer>customerRowMapper = (rs, i)->{
Integer id = rs.getInt ("id");
String firstName = rs.getString ("first_name");
String lastName = rs.getString ("last_name");
return new Customer (id, firstName, lastName);
};
public List<Customer>findAll () {
List<Customer>Customers = jdbcTemplate.query ("SELECT id, first_name FROM customers ORDERBY id", customerRowMapper);
return Customers;
}
public Customer findOne (Integer id) {
SqlParameterSource param = new MapSqlParameterSource (). addValue ("id", id);
return jdbcTemplate.queryForObject ("SELECT id, first_name, last_name FROM customers WHERE id =: id", param, customerRowMapper);
}
public Customer save (Customer customer) {SqlParameterSource param = new BeanPropertySqlParameterSource (customer);
if (customer.getId () == null) {
jdbcTemplate.update ("INSERT INTO customers (first_name, last_name) values (: firstName,: lastName,)", param);
} else {
jdbcTemplate.update ("UPDATE customers SET first_name =: first_name, last_name =: last_name WHERE is =: id", param);
}
return customer;
}
public void delete (Integer id) {
SqlParameterSource param = new MapSqlParameterSource (). addValue ("id", id);
jdbcTemplate.update ("DELETE FROM customers WHERE id =: id", param);
}
}
Pom.xml
<? xml version = "1.0" encoding = "UTF-8"?><project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent> <groupId>org.springframework.boot</groupId> spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath /><!-lookup parent from repository-></parent><groupId>com.example</groupId> hajiboot-layering</artifactId><version>1.0.0-SNAPSHOT</version><name>hajiboot-layering</name><description>Demo project for Spring Boot</description><properties> <java.version>11</java.version></properties><dependencies> <dependency> <groupId>org.lazyluke</groupId> log4jdbc-remix</artifactId> <version>0.2.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> h2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> spring-boot-starter-data-rest</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId> spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.projectreactor</groupId> reactor-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> lombok</artifactId> <version>1.181.12</version><!-$NO-MVN-MAN-VER $-> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency></dependencies><build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> spring-boot-maven-plugin</artifactId> </plugin> </plugins></build></project>
-
Answer # 1
Related articles
- java - i want to eliminate the error that occurs when starting an application that uses jsonic
- java - springboot 404 whitelabel error page
- i want to eliminate the parse error of windows batch
- ruby on rails - i want to eliminate the error when deploying to heroku
- when i run /gradlew eclipse, i get an error if compilejava --release is an invalid flag
- java - when i run it with springboot and access the web, i get a white label error page
- java - error: incompatible type: unable to convert string [] to string
- i want to resolve a java error
- fighting ice execution error [java]
- java - an error occurs when executing the jar file
- ruby - (rails) i want to eliminate the error "errno :: eacces in ~" and "permission denied @ rb_sysopen ~"
- java - i want to get the entire list from the db and display it, but an error occurs when executing the application
- i want to eliminate the error when connecting to mysql on ec2 from sequel pro
- java - web-app shows error in webxml
- java - how to use springboot h2db in server mode
- java bigdecimal acceleration error when dividing by 0
- c - eliminate error messages
- i get the following error in java code please tell me what is wrong
- i want to eliminate the era-java implementation [javalangillegalargumentexception]
Trends
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
Add spring-boot-starter-validation to pom.xml.