First off: Yes I have searched for previous questions. None of the answers work unfortunately.
I have created an entity class Product, a repository class ProductRepository and a main class Application.
Below you find the code:
Application
#SpringBootApplication
public class Application {
public static void main (String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
ProductRepository repo = ctx.getBean(ProductRepository.class);
Product product = new Product();
product.setDescription("HDD");
repo.save(product);
}
}
Product
#Entity
#Table(name="Products")
public class Product {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int product_id;
private String description;
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
ProductRepository
#Repository
public interface ProductRepository extends CrudRepository<Product, Integer> {
}
application.properties file
The file looks like this:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:8080/entmob
spring.datasource.username=entmob
spring.datasource.password=*******
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.show_sql=true
spring.jpa.hibernate.hbm2ddl.auto=create
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-relational-data-access</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>[5,]</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Compiling gives zero errors. However, the database does not get created in my localhost. I'm using XAMPP to manage my localhost.
Could somebody provide any help? Many thanks!
<property name="hibernate.hbm2ddl.auto">create</property>
will create tables. But it will not create database. Change the connection url to generate the database as below.
jdbc:mysql://localhost:8080/entmob?createDatabaseIfNotExist=true
Use the below connector in pom.xml and above connection url.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version> <!-- or 5.1.28 / 5.1.30 -->
</dependency>
Here is the solution. As i suggested in the comment section:
I would first try to remove the H2 dependency from the pom.xml and
try . I suspect that your db is embedded.
When springboot finds the h2 dependency it will consider it as an
webapp with embedded db and will ignore your mysql config
Related
Im trying to learn spring-boot basic application and unable to solve this error
APPLICATION FAILED TO START
Description:
Field dao in com.car.services.CarServices required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.
repository file
#SuppressWarnings("unused")
#Transactional
#Repository
public class CarDAO implements ICarDAO {
#PersistenceContext
private EntityManager entityManager;
#SuppressWarnings("unchecked")
#Override
public List<Car> getCars() {
//String hql = "FROM Car ";
String hql = "FROM Car as a ORDER BY a.id DESC";
return (List<Car>) entityManager.createQuery(hql).getResultList();
}
#Override
public Car getCar(int carId) {
return entityManager.find(Car.class, carId);
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.car</groupId>
<artifactId>car</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>car-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
service file
#Service
public class CarServices implements ICarServices {
#Autowired
private ICarDAO dao;
#Override
public List<Car> getCars() {
return dao.getCars();
}
#Override
public Car getCars(int carId) {
return dao.getCar(carId);
}
}
Controller file
#RestController
#RequestMapping("carservice")
public class CarController {
#Autowired
private ICarServices service;
#GetMapping("car")
public ResponseEntity<List<Car>> getCars(){
List<Car> cars = service.getCars();
return new ResponseEntity<List<Car>>(cars, HttpStatus.OK);
}
}
main function
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class CarDemoApplication {
public static void main(String[] args) {
SpringApplication.run(CarDemoApplication.class, args);
System.out.println("hello from car");
}
}
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/cardb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
I tried
import org.springframework.beans.factory.annotation.Autowired;
in my repository.java file but it didnt work for me.
You need to remove DataSourceAutoConfiguration.class from exclusions. That is what sets up the EntityManagerFactory.
So this:
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
// Main class definition
Should be:
#SpringBootApplication()
// Main class definition
I started watching a Tutorial on how to make a very basic Spring Boot Project. https://www.youtube.com/watch?v=vtPkZShrvXQ
No matter what i do i always get error 401, even when i copy the code 1-1.
Thank you in advance for taking your time to help me.
I tried putting this into the application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/LoginDB
spring.datasource.username=root
spring.datasource.password=
I made following Files
Folder structure
Player
package com.Project_A.Databaseconnection.Artifact.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.UUID;
public class Player {
private final UUID id;
private final String PlayerName;
public Player(#JsonProperty("id") UUID id,
#JsonProperty("playerName") String playerName
) {
this.id = id;
PlayerName = playerName;
}
public UUID getId() {
return id;
}
public String getPlayerName() {
return PlayerName;
}
}
PlayerDao
package com.Project_A.Databaseconnection.Artifact.dao;
import com.Project_A.Databaseconnection.Artifact.model.Player;
import java.util.List;
import java.util.UUID;
public interface PlayerDao {
int insertPlayer(UUID id, Player player);
default int insertPlayer(Player player){
UUID id = UUID.randomUUID();
return insertPlayer(id, player);
}
List<Player> selectAllPlayer();
}
FakePlayerDataAccessService
package com.Project_A.Databaseconnection.Artifact.dao;
import com.Project_A.Databaseconnection.Artifact.model.Player;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
#Repository("fakeDao")
public class FakePlayerDataAccessService implements PlayerDao {
private static List<Player> DB = new ArrayList<>();
#Override
public int insertPlayer(UUID id, Player player){
DB.add(
new Player(id,
player.getPlayerName()
//player.getPlayerPassword()
)
);
return 0;
}
#Override
public List<Player> selectAllPlayer() {
return DB;
}
}
PlayerController
package com.Project_A.Databaseconnection.Artifact.api;
import com.Project_A.Databaseconnection.Artifact.model.Player;
import com.Project_A.Databaseconnection.Artifact.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
#RequestMapping("api/v1/player")
#RestController
public class PlayerController {
private final PlayerService playerService;
#Autowired
public PlayerController(PlayerService playerService){
this.playerService = playerService;
}
#PostMapping
public void addPlayer(#RequestBody Player player) { playerService.addPlayer(player); }
#GetMapping
public List<Player> getAllPlayer() { return playerService.getAllPlayer(); }
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Project_A.Databaseconnection</groupId>
<artifactId>Artifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LoginServerConnector</name>
<description>connection with the LoginServer</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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-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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
If you don't need spring security in your project then remove below 2 dependencies from your pom.xml file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
And if you need basic security then only use 2nd dependency And default user name is "user" and password you will get from console log.
I'm building a small desktop app using JavaFX and Spring Boot. I used JavaFX Weaver to integrate JavaFX and Spring Boot together.
I want to use the JPA repository Interface to access a MySQL database, however, I get an error when starting the app because of the auto wiring of the repository field.
Console Error
Project Structure
Service
#Service
public class ColorService {
// I get the error when I add these 2 lines of code
#Autowired
private ColorRepository colorRepository;
public List<Color> getColors(){
return colorRepository.findAll();
}
}
Repository
#Repository
public interface ColorRepository extends JpaRepository<Color, Integer> {
}
Entity
#Entity
#Table(name="color")
public class Color {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="color_id")
private int id;
#Column
private String colorName;
public Color() {
}
public Color(String colorName) {
this.colorName = colorName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getColorName() {
return colorName;
}
public void setColorName(String colorName) {
this.colorName = colorName;
}
}
Main App
#SpringBootApplication
public class MainApp {
public static void main(String[] args) {
Application.launch(JavaFxApplication.class, args);
}
}
JavaFX Main App
public class JavaFxApplication extends Application {
private ConfigurableApplicationContext applicationContext;
#Override
public void init() {
String[] args = getParameters().getRaw().toArray(new String[0]);
this.applicationContext = new SpringApplicationBuilder().sources(MainApp.class).run(args);
}
#Override
public void start(Stage stage) {
FxWeaver fxWeaver = applicationContext.getBean(FxWeaver.class);
Parent root = fxWeaver.loadView(MainController.class);
Scene scene = new Scene(root);
scene.getStylesheets().add("css/style.css");
stage.setScene(scene);
stage.show();
}
#Override
public void stop() {
this.applicationContext.close();
Platform.exit();
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.diogo</groupId>
<artifactId>habittracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>habittracker</name>
<description>Habit Tracker</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>net.rgielen</groupId>
<artifactId>javafx-weaver-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>com.diogo.habittracker.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
The package of your class ColorRepository is not defined in component scan.
try to add : #ComponentScan({"package1","package2","..."})
Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: oracle.jdbc.driver.OracleDriver
pom.xml file
https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
<groupId>com.Projectdashboardtool</groupId>
<artifactId>Project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-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-jersey</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-services</artifactId>
</dependency> -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Controller class
#RestController
#RequestMapping("/project")
public class ProjectController {
#Autowired
private ProjectRepository projectRepository;
#GetMapping
public List<Project> list(){
//List<Project> project=new ArrayList<>();
return projectRepository.findAll();
}
#PostMapping
#ResponseStatus(HttpStatus.OK)
public void create(#RequestBody Project project) {
projectRepository.save(project);
}
#GetMapping("/{id}")
public Project get(#PathVariable("id") long id) {
return projectRepository.getOne(id);
}
}
Model class
package com.Projectdashboardtool.Project.Model;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
#Entity
#JsonIgnoreProperties({"hiberanateLazyInitializer", "handler"})
public class Project {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String dg_number;
private String project;
private String release_level;
private String release_area;
private String release_number;
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd-MM-yyyy")
private Date planned_release_date;
private Date release_date;
public String getDg_number() {
return dg_number;
}
public void setDg_number(String dg_number) {
this.dg_number = dg_number;
}
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
public String getRelease_level() {
return release_level;
}
public void setRelease_level(String release_level) {
this.release_level = release_level;
}
public String getRelease_area() {
return release_area;
}
public void setRelease_area(String release_area) {
this.release_area = release_area;
}
public String getRelease_number() {
return release_number;
}
public void setRelease_number(String release_number) {
this.release_number = release_number;
}
public Date getPlanned_release_date() {
return planned_release_date;
}
public void setPlanned_release_date(Date planned_release_date) {
this.planned_release_date = planned_release_date;
}
public Date getRelease_date() {
return release_date;
}
public void setRelease_date(Date release_date) {
this.release_date = release_date;
}
public String getRelease_type() {
return release_type;
}
public void setRelease_type(String release_type) {
this.release_type = release_type;
}
private String release_type;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Override
public String toString() {
return "Project [id=" + id + ", dg_number=" + dg_number + ", project=" + project + ", release_level="
+ release_level + ", release_area=" + release_area + ", release_number=" + release_number
+ ", planned_release_date=" + planned_release_date + ", release_date=" + release_date
+ ", release_type=" + release_type + "]";
}
}
application.properties file
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect
spring.datasource.url=jdbc:oracle:thin:#//hostname/servicename
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
Adding dependency :
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.1.0</version>
</dependency>
And correct hibernate dialect - org.hibernate.dialect.OracleDialect should work.
I think you are using property file for DB oracle. From your exception i can able to see spring boot failed to load oracle driver. So download ojdbc6.jar from Oracle website or if you install oracle11g in your local system you can find ojdbc in jdbc/lib folder.
add this to your project, On eclipse right click on project --> build path --> add this jar.
So suppose you are using spring boot add this dependency to your pom.xml
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>//add letest version
</dependency>
Suppose if your dependency not detected by maven follow the below steps:-
Run this command
mvn install:install-file - Dfile=C:\Users\user\.m2\repository\com\oracle\ojdbc6\11.2.0\ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
Then You can Use this.
Oracle JDBC drivers are available on Central Maven. Check out this blog. You can add this GAV for 18.3 JDBC driver.
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.3.0.0</version>
As is clear from the topic of the question, this is my stack of technologies. I have a problem encoding Russian characters.
Its my controller:
#RestController
public class ProductController {
#Autowired
private ProductService productService;
#RequestMapping("/Product/ByQuery")
public List getProductsByQuery(#RequestHeader(name = "query" , required = false) String query) {
System.out.println(new String(query.getBytes(),StandardCharsets.ISO_8859_1));
return productService.findByQuery(new String(query.getBytes(),StandardCharsets.ISO_8859_1));
}
}
its my Service :
public class ProductServiceImpl implements ProductService {
#Autowired
private ProductRepository productRepository;
#Override
public List<Product> findByQuery(String query) {
List<DataModel.Product> productList = productRepository.findDistinctByNameContainingIgnoreCaseOrCategoryContainingIgnoreCaseOrManufacturerContainingIgnoreCaseOrInformationValueContainingIgnoreCase(query,query,query,query);
List<Product> resultProducts = new ArrayList<>();
ModelMapper modelMapper = new ModelMapper();
for (int i = 0; i < productList.size(); i++) {
int like = 0;
int dislike = 0;
for (Rating rating : productList.get(i).getRating()) {
if (rating.getType().equals("Like")) {
like += 1;
} else {
dislike += 1;
}
}
resultProducts.add(modelMapper.map(productList.get(i), Product.class));
resultProducts.get(i).setCommentCnt(productList.get(i).getComment().size());
resultProducts.get(i).setLike(like);
resultProducts.get(i).setDislike(dislike);
resultProducts.get(i).setImage(productList.get(i).getImage().get(0).getUrl());
}
return resultProducts;
}
}
Repository:
#Repository
public interface ProductRepository extends JpaRepository<Product,Long> {
List<Product> findDistinctByNameContainingIgnoreCaseOrCategoryContainingIgnoreCaseOrManufacturerContainingIgnoreCaseOrInformationValueContainingIgnoreCase(String name,String category, String manufacturer, String value);
}
Application :
#SpringBootApplication(scanBasePackages = {"Service", "repository", "controller"})
#EntityScan("DataModel")
#EnableJpaRepositories(basePackages = "repository")
public class CloudliquidApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(CloudliquidApplication.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
#Bean
public ProductService productService() {
return new ProductServiceImpl();
}
#Bean
public TomcatServletWebServerFactory tomcatFactory() {
return new TomcatServletWebServerFactory() {
#Override
protected void postProcessContext(Context context) {
((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
}
};
}
}
Property :
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
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.lopamoko</groupId>
<artifactId>cloudliquid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloudliquid</name>
<description>Demo project for Spring Boot</description>
<pluginRepositories>
<pluginRepository>
<id>maven-annotation-plugin-repo</id>
<url>http://maven-annotation-plugin.googlecode.com/svn/trunk/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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</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-data-jpa</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.4</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
What do I need to do, so that Russian characters will finally begin to be perceived?
Before moving to the spring, it helped me - new String (source.getBytes (), StandardCharsets.UTF-8)). But after the move it stopped working (in any format).
What else should you try? If anything, I transfer the data from Android (Retrofit).
move String query from #RequestHeader to #RequestParam