I'm Stuck with JPA since two days , im trying to save a new Object to database but i got no luck with it, it's just throw no exception so i don't know what is going on. why my code is not working
here is the controller
#RestController
public class RealEstatesViewController {
#Autowired
private RealEstateRepository realEstatesViewRepository;
#RequestMapping(value = "/realEstatesView", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<String> addRealEstatesClass(#RequestBody ArrayList<RealEstatesView> realEstatesViews)
throws Exception {
realEstatesViewRepository.save(realEstatesViews);
return new ResponseEntity<>(HttpStatus.OK);
}
}
here is my Model i implemented my columns with my tables
#Entity
#Table(name = "real_estates_views")
public class RealEstatesView {
#GeneratedValue
#Id
#Column(name = "real_estate_view_id")
private int realEstateViewId;
#Column(name = "real_estate_offer_id")
private int realEstateOfferId;
#Column(name = "real_estate_view_date")
private String realEstateViewDate;
#Column(name = "real_estate_view_time")
private String realEstateViewTime;
}
here is my repo
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.akarat.demo.model.RealEstatesView;
#Repository
public interface RealEstateRepository extends CrudRepository<RealEstatesView, Integer> {
}
and 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>
<groupId>com.example</groupId>
<artifactId>AkaratDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AkaratDemo</name>
<description>Demo project for Akarat</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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-actuator</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-actuator-docs</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>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.8-dmr</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
finally the resource folder
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3307/demo?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Related
I faced a problem with importing the Spring Security library.
Here is my main class:
package com.example.test.myfirstproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
#SpringBootApplication
public class MyFirstProjectApplication {
public static void main(String[] args) {
SpringApplication.run(MyFirstProjectApplication.class, args);
}
#Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
And my pom.xml file:
<?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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.test</groupId>
<artifactId>myfirstproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myfirstproject</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-security</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</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>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
When I'm trying to make a build in IntelliJ IDEA I get errors:
java: package org.springframework.security.crypto.bcrypt does not exist
java: cannot find symbol
symbol: class BCryptPasswordEncoder
location: class com.example.test.myfirstproject.MyFirstProjectApplication
I used autocomplete for method BCryptPasswordEncoder, so there shouldn't be a mistake. I also tried to change the place of security dependency in pom.xml file, but it also didn't help.
Thank you for your help!
Good afternoon I am trying to generate a Rest service with java spring boot, but when trying to execute the jar I get the following error, which is giving me a headache and I don't know what it is specifically:
java.lang.NoSuchMethodError:
org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource.(Lorg/springframework/core/type/AnnotationMetadata;Ljava/lang/Class;Lorg/springframework/core/io/ResourceLoader;Lorg/springframework/core/env/Environment;)V
at
org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport$1.(AbstractRepositoryConfigurationSourceSupport.java:67)
at
org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.getConfigurationSource(AbstractRepositoryConfigurationSourceSupport.java:66)
at
org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.registerBeanDefinitions(AbstractRepositoryConfigurationSourceSupport.java:58)
at
org.springframework.context.annotation.ImportBeanDefinitionRegistrar.registerBeanDefinitions(ImportBeanDefinitionRegistrar.java:86)
at
org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:385)
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) at
org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:384)
at
org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:148)
at
org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120)
at
org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331)
at
org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236)
at
org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
at
org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
at
org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706)
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
at
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)
at
org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:295)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1112)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1101)
at
com.example.consulta.SpringBootSoapApp.main(SpringBootSoapApp.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
java.lang.NoSuchFieldException: findAnnotationCache at
java.lang.Class.getDeclaredField(Class.java:2070) at
org.springframework.boot.devtools.restart.Restarter.clear(Restarter.java:376)
at
org.springframework.boot.devtools.restart.Restarter.cleanupKnownCaches(Restarter.java:361)
at
org.springframework.boot.devtools.restart.Restarter.cleanupCaches(Restarter.java:348)
at
org.springframework.boot.devtools.restart.Restarter.access$000(Restarter.java:81)
at
org.springframework.boot.devtools.restart.Restarter$1.call(Restarter.java:170)
at
org.springframework.boot.devtools.restart.Restarter$1.call(Restarter.java:165)
at
org.springframework.boot.devtools.restart.Restarter$LeakSafeThread.run(Restarter.java:602)
Annex my code
package com.example.consulta;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
main.class
#SpringBootApplication
public class SpringBootSoapApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootSoapApp.class, args);
}
}
Controller
package com.example.consulta.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.example.consulta.entity.Cliente;
import com.example.consulta.service.ClienteService;
#RestController
public class ClienteController {
#Autowired
private ClienteService clienteService;
#GetMapping("/allCliente")
public List<Cliente> listarCliente(){
return clienteService.findAll();
}
#GetMapping("/cliente/{cedula}")
public Cliente detalleCliente(#PathVariable int cedula) {
return clienteService.findById(cedula);
}
}
DAO
package com.example.consulta.DAO;
import org.springframework.data.repository.CrudRepository;
import com.example.consulta.entity.Cliente;
public interface ClienteDAO extends CrudRepository<Cliente, Integer>{
}
Entity
package com.example.consulta.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="Cliente")
public class Cliente {
#Id
private int cedula;
private String nombre;
private String apellido;
private String tipo_cliente;
public int getCedula() {
return cedula;
}
public void setCedula(int cedula) {
this.cedula = cedula;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellido() {
return apellido;
}
public void setApellido(String apellido) {
this.apellido = apellido;
}
public String getTipo_cliente() {
return tipo_cliente;
}
public void setTipo_cliente(String tipo_cliente) {
this.tipo_cliente = tipo_cliente;
}
}
Service
package com.example.consulta.service;
import java.util.List;
import com.example.consulta.entity.Cliente;
public interface ClienteService {
public List<Cliente> findAll();
public Cliente findById(int cedula);
}
ServiceImpl
package com.example.consulta.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.consulta.DAO.ClienteDAO;
import com.example.consulta.entity.Cliente;
#Service
public class ClienteServiceImpl implements ClienteService{
#Autowired
private ClienteDAO clienteDao;
#Transactional
public List<Cliente> findAll() {
return (List<Cliente>) clienteDao.findAll();
}
public Cliente findById(int cedula) {
return clienteDao.findById(cedula).get();
}
}
pom.xml
<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.consulta</groupId>
<artifactId>ServiceConsultaCliente</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ServiceConsultaCliente</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>
</project>
application.properties
# Oracle settings
spring.application.name=prueba-microservicio-cliente
server.port=8001
spring.datasource.url=jdbc:oracle:thin:#//10.164.7.203:1521/ORCLPDB1.localdomain
spring.datasource.username=cesar
spring.datasource.password=xxxxx123
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.hibernate.ddl-auto=update
UPDATE After doing what specified by Marcos Barrero I get the following error:
error i get:
pom.xml (final)
<?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.2.1.RELEASE</version>
</parent>
<groupId>com.example.consulta</groupId>
<artifactId>ServiceConsultaCliente</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ServiceConsultaCliente</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-data-jpa</artifactId>
</dependency>
<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>
<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>
</project>
Try to add spring boot parent pom. With it you shouldn't specify starters versions - parent pom contains all information about needed versions.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
From what I can see your problem is the way you're importing the dependencies, the recommended way to do so is by using the Spring Boot's parent pom and its managed dependencies.
I recommend you to use start.spring.io to generate your next projects, here's a sample with the Spring Boot dependencies that you have, the major diff is the parent pom.
For your project, you need to add the extra dependencies such as oracle and any further configuration you have.
<?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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.consulta</groupId>
<artifactId>ServiceConsultaCliente</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ServiceConsultaCliente</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-data-jpa</artifactId>
</dependency>
<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>
<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>
</project>
UPDATE
Here's your version with h2 (you have to change it to oracle)
<?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.2.1.RELEASE</version>
</parent>
<groupId>com.example.consulta</groupId>
<artifactId>ServiceConsultaCliente</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ServiceConsultaCliente</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- change it to oracle driver -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</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>
<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>
</project>
Try use this pom.xml
It was generated by https://start.spring.io/ and I inserted your Oracle dependencies and repositories.
<?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.2.4.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.example.consulta</groupId>
<artifactId>ServiceConsultaCliente</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ServiceConsultaCliente</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</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>
<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>
<repositories>
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>
When you face NoSuchMethodException at runtime, it generally means 'classpath hell' - e.g., for simplification, when you've got two version of the same library (one containing the method, but the other - not) and only god knows which version will actually get into your final build if you don't do anything.
To find such dependencies, you shoult run 'mvn dependency:tree' command and search for something related to 'org.springframework.boot.devtools'... => most probably you'll see two versions
Then - you should either use section of maven POM to strictly set the version, or, as guys suggested above - use spring boot parent (also, a more advanced solution is importing a BOM - which is kind of a set of rules for dependency versions).
I'm having troubles with returning a JSONObject in Java Spring. I've been googling fanaticly but haven't found a solution yet.
I'm using Jackson's fasterxml but I still can't get my API to respond in JSON. I'll include the files which I think are necessary to help.
First off I'll show my pom.xml, in which I think the error origins. There might be a collision between Jackson and another dependency, but I can't seem to figure out which one.
<?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>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The controller on which the error occurs goes as following. You should be looking at testMeth2()
#RestController
#RequestMapping(path="/test")
#EnableWebMvc
public class TestController {
#Autowired
private CourseRecommendation courseRecommendation;
#Autowired
private EmployeesRepository employeesRepository;
Employees emp;
#RequestMapping(value = "/check/1", method = RequestMethod.GET)
public #ResponseBody
Employees testMeth1() {
emp = employeesRepository.findById(1);
emp.setSex("F");
employeesRepository.save(emp);
return emp;
};
#RequestMapping(value = "/check/2", method = RequestMethod.GET)
public #ResponseBody
JSONObject testMeth2() {
return new JSONObject();
}
}
I will include the stacktrace aswell on pastebin:
https://pastebin.com/5BsEk9C7
This is in a project I'm doing for an internship and every bit help would be greatly appreciated!
Cheers.
I ended up just using a POJO, which Spring converted to a JSON for me. It's not the same, but its a good workaround for my project.
Cheers!
Here is my Application.java:
#Configuration
#EnableAutoConfiguration(exclude =
{JpaRepositoriesAutoConfiguration.class})
#ComponentScan
#SpringBootApplication
public class UserFrontApplication {
public static void main(String[] args) {
SpringApplication.run(UserFrontApplication.class, args);
}
}
Contents of application.properties:
spring.datasource.driverClassName=org.gjt.mm.mysql.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create
pom.xml:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.userFront</groupId>
<artifactId>userfront</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>UserFront</name>
<description>User frontend for online banking</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.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-web</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</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 application runs fine but no tables are created.
User class that has to be stored in database:
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "userId", nullable = false, updatable = false)
private Long userId;
}
Why you are using org.hibernate.dialect.PostgreSQLDialect. you need to update dialect as per your database, update these two properties.
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
In your main class, try adding
#EntityScan(basePackages = {"packagename_where_entity_class_is_present"})
Hope it works!
OR
You can try reordering the packages i.e. for example, if your main class is in the package com.main, then let your entity class be in the package com.main.entity
I am trying to implement Solr into my Java Spring application, however I have troubles with dependencies, since STS cannot find SolrClient and MultiCoreSolrServerFactory. Here is the class that is causing me troubles:
package lt.gerasolutions.gsm.core.classifiers.domain;
import javax.annotation.Resource;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.SolrCrudRepository;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
import org.springframework.data.solr.server.support.HttpSolrClientFactoryBean;
#Configuration
#EnableSolrRepositories(basePackages={"lt.gerasolutions.gsm.core.solrRepositories"},
multicoreSupport=true)
public class SolrContext {
static final String SOLR_HOST = "solr.host";
#Resource
private Environment environment;
#Bean
public SolrServer solrServer() {
String solrHost = environment.getRequiredProperty(SOLR_HOST);
SolrServer server = new HttpSolrServer(solrHost);
MulticoreSolrServerFactory factory;
//I was told to use this factory to return a server, but eclipse cannot find it unfortunately.
}
#Bean (name = "EN")
public SolrTemplate solrEn(){
return new SolrTemplate(solrServer());
//The type org.apache.solr.client.solrj.SolrClient cannot be resolved. It is indirectly referenced from required .class files
}
#Bean (name = "LT")
public SolrTemplate solrLt(){
return new SolrTemplate(solrServer());
}
}
My dependencies are managed by maven, and this is its pom file:
<?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>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>a</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
spring-boot-starter-data-elasticsearch
</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>6.0.0</version>
<exclusions>
<exclusion>
<artifactId>jdk.tools</artifactId>
<groupId>jdk.tools</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Check the solrj jar version in dependencies.
Since spring boot is having a dependency of 4.10.4, new version doesnt get override. The old version does not have Cloud classes.
You can manually add
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.5.1</version>
</dependency>
You will have new version then.