Dijital Ocean spaces listObjects not working - java

My application.yml
## PostgreSQL
spring:
datasource:
url: jdbc:postgresql://myip:5432/db?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
username: wqeqe
password: qweqwewqe$qwewqe
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true
---
server:
port: 8085
## DO properties
do:
spaces:
key: mykey
secret: mysecret
endpoint: digitaloceanspaces.com
region: myregion
bucket: mybusket
My read config read appication.yml
#Configuration
#PropertySource("classpath:application.yml")
#ConfigurationProperties(prefix = "do.spaces")
public class DoConfig {
#Value("${key}")
private String doSpaceKey;
#Value("${secret}")
private String doSpaceSecret;
#Value("${endpoint}")
private String doSpaceEndpoint;
#Value("${region}")
private String doSpaceRegion;
#Bean
public AmazonS3 getS3() {
BasicAWSCredentials creds = new BasicAWSCredentials(doSpaceKey, doSpaceSecret);
return AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(doSpaceEndpoint, doSpaceRegion))
.withCredentials(new AWSStaticCredentialsProvider(creds)).build();
}
}
My service
#Override
public List<TemplateDto> getAll() {
return (List<TemplateImage>) templateImageRepository.findAll();
}
My repo
#Repository
public interface TemplateImageRepository extends PagingAndSortingRepository<TemplateImage,Long> {
}
Hi everyone I am using this project dijital ocean spaces but the problem listObject images don't return null. What is the problem please help me I am new working is the file servers.
Thanks !!!

Related

Spring Boot how to configure list of Hikari datasources dynamically

If I only have one datasource and I want to configure it with Hikari then in application.yml I can do something like this:
spring:
datasource:
app:
platform: POSTGRESQL
url: jdbc:postgresql://localhost:5432/mydb
username: mydbuser
password: mydbpass
hikari:
maximum-pool-size: 15
leak-detection-threshold: 15000
max-lifetime: 900000
data-source-properties: stringtype=unspecified
pool-name: defaultHikariPool
And in code I do this:
#Bean
#Primary
#ConfigurationProperties("spring.datasource.app")
fun appDataSourceProperties(): DataSourceProperties {
return DataSourceProperties()
}
#Bean
#ConfigurationProperties(prefix = "spring.datasource.app.hikari")
fun appDataSource(): HikariDataSource {
return appDataSourceProperties()
.initializeDataSourceBuilder()
.type(HikariDataSource::class.java)
.build()
}
This works very well. But let's say I have N datasources where size of N can vary depending on deployment. I created a block like this in the applicaiton.yml:
my-custom-parties:
parties:
- my-custom-party-id: 1
datasource:
platform: POSTGRESQL
url: jdbc:postgresql://localhost:5432/my-db-1
username: user1
password: pass1
hikari:
pool-name: db1HikariPool
maximum-pool-size: 3
leak-detection-threshold: 5000
max-lifetime: 900000
data-source-properties: stringtype=unspecified
- my-custom-party-id: 2
datasource:
platform: POSTGRESQL
url: jdbc:postgresql://localhost:5432/my-db-2
username: user2
password: pass2
hikari:
pool-name: db2HikariPool
maximum-pool-size: 10
leak-detection-threshold: 20000
max-lifetime: 900000
data-source-properties: stringtype=unspecified
And I created a configuration class like this:
#Component
#ConfigurationProperties(prefix = "my-custom-parties")
class MyCustomParties {
private var parties: List<MyCustomParty> = listOf()
fun setParties(parties: List<MyCustomParty>) {
this.parties = parties
}
fun getParties() = parties
}
class MyCustomParty {
lateinit var myCustomPartyId: String
private var datasource: DataSourceProperties? = null
fun setDatasource(datasource: DataSourceProperties) {
this.datasource = datasource
}
fun getDatasource() = datasource?.initializeDataSourceBuilder()?.type(HikariDataSource::class.java)?.build()
}
This intializes a hikari datasource, but it doesn't pick up the configuration properties from the file.
I can't hardcode #Bean's for every datasource because the number of datasources is different in different deployments.
So how can I wire this together correctly?

Spring JPA - Repository Query is returning 0 rows

I have this repository class:
public interface ShiftRepository extends CrudRepository<Shift, Long> {
#Query("SELECT s FROM Shift s")
public List<Shift> getAllShifts();
}
All I want it to do is grab all shifts.
The Service calling it is just doing this:
public List<Shift> getAllShifts() {
return shiftRepository.getAllShifts();
}
And the Controller is just doing this:
public ResponseEntity<List<Shift>> getAllShifts() {
List<Shift> shifts;
try {
shifts = shiftService.getAllShifts();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ArrayList<>());
}
if (shifts != null) {
System.out.println(shifts.size());
return ResponseEntity.status(HttpStatus.OK).body(shifts);
} else {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ArrayList<>());
}
}
On the frontend I am getting this response.
Empty array of shifts
succesful 200 response
So I don't know what the issue is, I just want all the records. Using Spring boot and Heroku Postgres
application.yml:
spring:
jpa:
properties:
hibernate.default_schema: dev_env
hibernate:
ddl-auto: create-drop
database-platform: org.hibernate.dialect.PostgreSQLDialect
datasource:
hikari:
schema: dev_env
driverClassName: org.postgresql.Driver
type: org.apache.tomcat.jdbc.pool.DataSource
Your problem seems like in that Query annotation. You have to define value tag before your query. As:
#Query(value = "SELECT s FROM Shift s")
Sorry for wasting time. It's late was very enthralled in Spring boot and React debugging my application. Did not realize I had not populated database with test data XD

Validate schema programmatically using hibernate

In mose projects the way to run your java app with schema validation is with that configuration (when using spring):
spring.jpa.hibernate.ddl-auto=validate
I ran into a problem that I need to validate my schema at a specific times during running, is there any way to implement that?
I saw that hibernate managed it with the AbstractSchemaValidator,
I'm using spring with hibernate, and I didn't found any information how to deal with it, the only thing I found is How to validate database schema programmatically in hibernate with annotations?
, but it was removed in the older versions of spring-boot
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
any ideas?
This is solution, if your use case requires:
granular & explicit control of which part of the schema should be
validated
the need is to validate multiple schemas
the need is to validate schema that is not used by the service, on which scheduled validator is running
db connections used by application should not be influenced by validation in any way (meaning, you don't want to borrow connection from main connections pool)
If above applies for your needs, than this is example of how to do scheduled schema validation:
Sources
#SpringBootApplication
#EnableScheduling
#EnableConfigurationProperties(ScheamValidatorProperties.class)
public class SchemaValidatorApplication {
public static void main(String[] args) {
SpringApplication.run(SchemaValidatorApplication.class, args);
}
}
#ConfigurationProperties("schema-validator")
class ScheamValidatorProperties {
public Map<String, String> settings = new HashMap<>();
public ScheamValidatorProperties() {
}
public Map<String, String> getSettings() {
return this.settings;
}
public void setSome(Map<String, String> settings) {
this.settings = settings;
}
}
#Component
class ScheduledSchemaValidator {
private ScheamValidatorProperties props;
public ScheduledSchemaValidator(ScheamValidatorProperties props) {
this.props = props;
}
#Scheduled(cron = "0 0/1 * * * ?")
public void validateSchema() {
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(props.getSettings())
.build();
Metadata metadata = new MetadataSources(serviceRegistry)
.addAnnotatedClass(Entity1.class)
.addAnnotatedClass(Entity2.class)
.buildMetadata();
try {
new SchemaValidator().validate(metadata, serviceRegistry);
} catch (Exception e) {
System.out.println("Validation failed: " + e.getMessage());
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}
}
#Entity
#Table(name = "table1")
class Entity1 {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Entity1() {}
public Long getId() {
return id;
}
}
#Entity
#Table(name = "table2")
class Entity2 {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Entity2() {}
public Long getId() {
return id;
}
}
schema.sql
CREATE DATABASE IF NOT EXISTS testdb;
CREATE TABLE IF NOT EXISTS `table1` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `table2` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);
application.yml
spring:
cache:
type: none
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3309/testdb?useSSL=false&nullNamePatternMatchesAll=true&serverTimezone=UTC&allowPublicKeyRetrieval=true
username: test_user
password: test_password
testWhileIdle: true
validationQuery: SELECT 1
jpa:
show-sql: false
database-platform: org.hibernate.dialect.MySQL8Dialect
hibernate:
ddl-auto: none
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
properties:
hibernate.dialect: org.hibernate.dialect.MySQL8Dialect
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
hibernate.hbm2ddl.auto: validate
schema-validator:
settings:
connection.driver_class: com.mysql.cj.jdbc.Driver
hibernate.dialect: org.hibernate.dialect.MySQL8Dialect
hibernate.connection.url: jdbc:mysql://localhost:3309/testdb?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
hibernate.connection.username: test_user
hibernate.connection.password: test_password
hibernate.default_schema: testdb
docker-compose.yml
version: '3.0'
services:
db:
image: mysql:8.0.14
restart: always
ports:
- 3309:3306
environment:
MYSQL_ROOT_PASSWORD: test_password
MYSQL_DATABASE: testdb
MYSQL_USER: test_user
MYSQL_PASSWORD: test_password
If you want to let the SchemaValidator to reuse the connection configuration and the mapping information that are already configured in the project rather then defining them once again for schema validation, you should consider my solution such that you are DRY and don't need to maintain these configurations in two separate places.
Actually , what SchemaValidator requires is the Metadata instance which is only available during bootstrapping Hibernate . But we can use Hibernate Integrator API (as described in here) to capture it such that we can validate them later.
(1) Create SchemaValidateService which implements Hibernate Integrator API to capture Metadata. Also setup a #Scheduled method to validate the schema at desired time.
#Component
public class SchemaValidateService implements Integrator {
private Metadata metadata;
#Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
this.metadata = metadata;
}
#Override
public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
}
//Adjust the scheduled time here
#Scheduled(cron = "0 0/1 * * * ?")
public void validate() {
try {
System.out.println("Start validating schema");
new SchemaValidator().validate(metadata);
} catch (Exception e) {
//log the validation error here.
}
System.out.println("Finish validating schema....");
}
}
(2) Register SchemaValidateService to Hibernate
#SpringBootApplication
#EnableScheduling
public class App {
#Bean
public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(SchemaValidateService schemaValidateService) {
return (prop -> {
List<Integrator> integrators = new ArrayList<>();
integrators.add(schemaValidateService);
prop.put("hibernate.integrator_provider", (IntegratorProvider) () -> integrators);
});
}
}
Also, this solution should has better performance as it does not need to create a new database connection for validating schema each time as it can just grab the connection from the existing connection pool.

Spring boot application properties with different keys [duplicate]

This question already has answers here:
Spring Boot - inject map from application.yml
(8 answers)
Closed 4 years ago.
How can i define a Object to read my application.mysql-databases by key:
swaw:
stage: dev
ip: x.x.x.x
port: 3306
databases:
mysql:
url: "jdbc:mysql://...."
password:
dba: AAA
read: BBB
mssql:
url: "jdbc:mssql://...."
password:
dba: CCC
read: DDD
informix:
....
I try with this object:
#ConfigurationProperties(prefix = "swaw.databases")
public class Databases {
private Map<String, DatabasesConfig> map;
public static class DatabasesConfig {
private String url;
private Password password;
//GETTER AND SETTER
i get per request: {"ip":"1x.x.x.x","port":"3306","databases":null}
#emoleumassi - Try this one :
#ConfigurationProperties(prefix = "databases")
public class Databases {
private String url;
private Password password;
//GETTER AND SETTER
}

Add new conection to database in Jhipster

I need to do a new database connection in a Jhipster project, I am doing the next
Add in application-dev.yml:
datasource:
driver-class-name: org.postgresql.ds.PGSimpleDataSource
url: jdbc:postgresql://localhost:5432/analytics
name: analytics
username: elser
password: ******
datasources:
elser:
driver-class-name: org.hibernate.dialect.PostgreSQLDialect
url: jdbc:postgresql://localhost:5432/elser
name: elser
username: elser
password: ******
And in DatabaseConfiguration.java:
#Bean
#ConfigurationProperties(prefix="spring.datasources.elser")
public DataSource dataSourceElser() {
return DataSourceBuilder.create().build();
}
I added a new class to test this:
#Inject
#Qualifier("dataSourceElser")
private DataSource dataSourceElser;
private JdbcTemplate jdbcTemplate;
#PostConstruct
public void init() {
jdbcTemplate = new JdbcTemplate(dataSourceElser);
int rowCount = this.jdbcTemplate.queryForObject("select count(*) from commons.usuario", Integer.class);
System.out.println(rowCount);
}
But its giving me the next error:
java.sql.SQLException: org.hibernate.dialect.PostgreSQLDialect cannot be cast to java.sql.Driver
You are currently specifying a hibernate dialect, not the JDBC driver name. You need to use:
driver-class-name: org.postgresql.Driver
Instead of both driver-class-name: org.postgresql.ds.PGSimpleDataSource and driver-class-name: org.hibernate.dialect.PostgreSQLDialect

Categories