Showing posts with label Spring Boot. Show all posts
Showing posts with label Spring Boot. Show all posts

Thursday, 8 July 2021

Configure/ Enable Swagger API for Spring Boot 2 (2.5.2)

 Enabling Swagger API documentation in Spring Boot 2.5.2 is pretty simple now.

We just need to few dependency and we are good to go

For Spring Boot Applications, If you are migrating to 2.5.2 version then perform the following operations

1. Remove library inclusions of earlier releases. Specifically remove springfox-swagger2 and springfox-swagger-ui inclusions.

2. Remove the @EnableSwagger2 annotations

3. Add the springfox-boot-starter as shown below

4. No need to external config class to configure docket-api. It is handled automatically.


Following is the dependency we have to add in our pom.xml (for maven project)


That's it!!!

and swagger-ui location has moved from 
http://host/context-path/swagger-ui.html 
to 
http://host/context-path/swagger-ui/index.html 
OR http://host/context-path/swagger-ui/

as shown below




The complete code can be found in my github

Saturday, 5 June 2021

Transaction Propagation in Spring, Spring Boot

What is Transaction Propagation ?

Propagation defines our business logic's boundary. Spring manages to start and pause a transaction based on our propagation setting.

In Spring Boot, we enable the transaction propagation using @Transactional annotation.

@Transactional : It Describes a transaction attribute on an individual method or on a class. 

We can set propagation, isolation, timeout, read-only and rollback conditions for our transaction using this annotation.

If annotation is applied at class level, then spring consider it for all the public methods, but if we applied at private or protected method then it ignores without an error.

Ex : 

  @Transactional(propagation = Propagation.REQUIRES_NEW)

  public void deleteExistingData() {
  }

Following are the settings for propagation

1.REQUIRED -  Default. Support a current transaction, create a new one if none exists.

2.SUPPORTS - Support a current transaction, execute non-transactionally if none exists.

3.MANDATORY - Support a current transaction, throw an exception if none exists. 

throw IllegalTransactionStateException

4.REQUIRES_NEW - Creates a new transaction, and suspend the current transaction if one exists.

5.NOT_SUPPORTED - Execute non transitionally, suspend the current transaction if one exists.

6.NEVER - Execute non-transitionally, throw an exception if a transaction exists.

7.NESTED - Execute within a nested transaction if a current transaction exists.

Thursday, 13 June 2019

Exclude jar from war in a Spring Boot gradle project

Spring Boot version : 2.1.4.RELEASE

If you would like to use jar only for compilation purpose and exclude the jar at run time or not part of the war then you can do as follows

In the gradle.build properties file use "compileOnly files" Option

dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("io.springfox:springfox-swagger2:2.9.2")
    implementation("io.springfox:springfox-swagger-ui:2.9.2")
compileOnly files('libs/<some>-api.jar')
}

As mentioned in the above dependencies , <some>-api.jar placed under libs folder will be used only for compile time and it will be not part of the war


Friday, 12 April 2019

Spring Boot application as a windows service

In this Blog, I will try to illustrate steps to run Spring Boot application as a Windows Service.

To run Spring Boot application as a windows service we will be using
 winsw: Windows service wrapper

Please follow below mentioned steps

Step1: Check out the version of Microsoft .NET framework to determine correct winsw version for the windows operating system.




To check Microsoft.NET framework version, Navigate to command prompt and run below mentioned command as shown below

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\full" /v version



If your .NET framework version is less than 3.5 then we need to download winsw v2.1.1 else we can download winsw v2.1.2


Step2: Download the winsw and prepare required directory structure.
We can download the winsw from below mentioned link


From the above mentioned git hub repository download the WinSW.NET4.exe and sample-minimal.xml to a directory and name it “spring-boot-service”

Also rename WinSW.NET4.exe and sample-minimal.xml to spring-boot-service.exe and spring-boot-service.xml


Place  your application war in the same folder (for example spring-boot-service.war)

Modify the spring-boot-service.xml file with proper values

As shown in above screen shot, give unique id for your service (For ex : spring-boot-service)
For the "executable" property give value till JAVA_HOME/java (i.e. folder where your java.exe file is present)

set the argument property as shown below
<arguments>-Dserver.port=9002 -Duser.timezone=UTC -Xms1024m -Xmx2048m -Dlogging.root=C:\test\spring-boot-service\logs\  -jar spring-boot-service.war</arguments> 


Step3: winsw installation and configuration set up
             Open command prompt as an administrator and navigate to directory where you placed winsw installation and configuration file as explained in the Step1 (for example spring-boot-service folder)
i) Execute spring-boot-service.exe install 

ii)                 Open windows “Services” as Administrator –
For Example in Windows 7, following is navigation path to launch windows Services
Start -> Control Panel -> System and Security -> Administrative Tools -> Services(Run as Administrator)

i)                 Now in the Services Window, select the service, right click and start

or in the command prompt run net start <service-name>
 where service-name is unique id which you given in service.xml file

To delete a process run below sc command
sc delete <service name> 

Wednesday, 17 October 2018

Serving static files from Spring Boot web app via Gateway application using Spring Cloud Zuul Proxy


Loading static files like image, css and js files via Spring Cloud Zuul Proxy is tricky part.

We will place our static resources under src- main – resources – static folder.

If we run our spring boot application, we can able to access the static resources present under src- main – resources – static folder.

But same spring boot application when we are trying to access via Zuul proxy we will get

GET http://<host>/js/welcome.js?v=1539843529723 net::ERR_ABORTED 404 error

In order to resolve this issue, one work around is to place all your static file under application-name/ application context configured in gateway application.

For example, in your gateway application you configured your spring boot application(demo) in application.properties file as follows

zuul.routes.demo.url=http://localhost:8091


But your static resources like img,css and js files are present under


src- main – resources – static folder.

Then create folder named demo under src- main – resources – static and then place all your static files under “demo” folder

So, when configuring zuul proxy we need place all our static resources under src-main-resources-static-<application.name>(folder)


One more alternative approach which I found was

  Creating a mapping in your gateway applications application.yml file as shown below

  zuul:
  routes:
    demo-viewer:
      path: /demo-viewer/**
      url: http://localhost:9099
      strip-prefix: true
    demo-viewer-img:
      path: /demo/**
      url: http://localhost:9099
      strip-prefix: false
    demo-viewer-boot:
      path: /bootstrap*/**
      url: http://localhost:9099
      strip-prefix: false
    demo-viewer-jquery:
      path: /jquery*/**
      url: http://localhost:9099
      strip-prefix: false
server:
  port: 8080
ribbon:
 eureka:
   enabled: false   

As you see, I have mentioned above mapping in my application.yml file. Here I am explicitly mapping route for my static resources like img, jquery and bootstrap



If any one knows better alternative, please suggest in comments.

Hope it helps!