Docker compose How to run local pubsub inside docker? Here is a docker compose file that will: pull in the google/cloud-sdk:emulators image initialize the project with id test-project-id create topic with name test-v1-topic create subscription with name test-v1-subscription version: '3.8' services: pubsub-emulator: image: google/cloud-sdk:emulators container_name: pubsub-emulator ports: - "8085:8085" command: gcloud beta emulators pubsub start … Continue reading Local pubsub emulator via docker compose && Spring Boot
Category: Java
Hibernate JPA and PostgreSQL: change type from String to text (@Lob)
In existing table with data populated, changing the column type from string to text requires some migration. For large data type, Postgres stores the data in a separate location and just references it by using an ID. This means that, until then, column data would be a string content and after the change, it would … Continue reading Hibernate JPA and PostgreSQL: change type from String to text (@Lob)
Failing tests with: InvalidDefinitionException: Java 8 date/time type `java.time.Instant` not supported by default
If issue is in the mock mvc tests, then just register module to object mapper... ObjectMapper o = new ObjectMapper(); o.registerModule(new JavaTimeModule());
Spring REST – returning image or a file
Just an example of how to return an image or a file from a rest controller. Idea can be to proxy image call or just to return a file. @GetMapping(value="/scans/{id}/{image}", produces = MediaType.IMAGE_JPEG_VALUE) public ResponseEntity<?> getScanImage(@PathVariable String id, @PathVariable String image) { final byte[] imageBytes = bucketApi.readScan(UUID.fromString(id), image); return new ResponseEntity<>(imageBytes, HttpStatus.OK); } @GetMapping(value="/scans/stream/{id}/{image}", produces … Continue reading Spring REST – returning image or a file
gRPC Spring Boot Server with Oauth2 (Keycloak)
This post will describe, in shortest steps, the configuration needed to get spring boot grpc server secured with oauth2 tokens, in my case with Keycloak. Resources: OAuth 2.0 Resource Server JWTgRPC Spring Boot StarterServer security configurationPostman gRPC My demo project can be found at github. Server code has been updated to have all the configuration … Continue reading gRPC Spring Boot Server with Oauth2 (Keycloak)
Hexagonal Architecture With SpringBoot
I have been trying to create a clean hexagonal architecture example with SpringBoot. More complex approaches are possible, by introducing the CQRS into the equation, but I believe that just over complicates the architecture and if there is no really good reason for it, I'd avoid it. There are a couple of examples out there … Continue reading Hexagonal Architecture With SpringBoot
Spring Boot JPA Test Splice
Here is a small example that: Uses @DataJpaTest annotation to bring in only configuration relevant for JPA tests (entities, repos etc...)Uses @TestMethodOrder(MethodOrderer.OrderAnnotation.class) and @Order annotations to enforce order of method executionUses @ComponentScan(basePackages = {"tech.dimitar.rosdjpaint.bootstrap"}) annotation to bring in the component that bootstraps the dataUses @Commit annotation on test to enforce transaction does not roll back … Continue reading Spring Boot JPA Test Splice
Spring Boot – Enable SQL logging
Enable sql logging, format and show bind parameters values, add to application.properties: # spring.jpa.show-sql=true # Show SQL spring.jpa.properties.hibernate.show_sql = true # Format SQL spring.jpa.properties.hibernate.format_sql=true # Show bind values! logging.level.org.hibernate.type.descriptor.sql=trace Example output (insert): Hibernate: call next value for hibernate_sequence Hibernate: insert into book (isbn, publisher, title, id) values (?, ?, ?, ?) 2022-05-17 19:57:10.916 TRACE 44505 … Continue reading Spring Boot – Enable SQL logging
Initialize a Database Using Hibernate – profile and db based
You want to initialize a database with different data depending on the data source? Should be straight forward: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization Spring Boot can automatically create the schema (DDL scripts) of your JDBC DataSource or R2DBC ConnectionFactory and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema.sql and data.sql, respectively. In addition, … Continue reading Initialize a Database Using Hibernate – profile and db based
Distributed lock on GCP Datastore
So, I needed a distributed lock on the GCP Datastore. In theory, it is all very simple: Get the lockDo what you need to doRelease the lock And, when I started thinking about it, it became clear that I need to have some kind of transaction mechanism in order to obtain the lock. Basically, when … Continue reading Distributed lock on GCP Datastore