Posts

Showing posts with the label Maven

Secure Spring Boot REST API using Basic Authentication

This is the third post of my Spring Boot Blog post series. In the very first post, I talked about my experience with creating RESTFul Services using Spring Boot. Then I have expanded the sample to integrate with Swagger documentation. In this post, I am going to expand above sample with security aspect.

What is API Security

API Security is a wide area with many different definitions, meanings, and solutions. The main key terms in API security are Authorization, Authentication, Encryption, Federation, and Delegation. However, I am not going to talk about each of them here.

What is Authentication

Authentication is used to reliably determine the identity of an end user and give access to the resources based on the correctly identified user.

What is Basic Authentication

Basic Authentication is the simplest way to enforce access controling to resources. Here, the HTTP user agent provides the username and the password when making a request. The string containing the username and password separ…

Integrating Swagger with Spring Boot REST API

Image
In the last post, I talked about my experience with creating RESTFul Services using Spring Boot. When creating a REST API, proper documentation is a mandatory part of it.

What is Swagger?

Swagger(Swagger 2) is a specification for describing and documenting a REST API. It specifies the format of the REST web services including URL, Resources, methods, etc. Swagger will generate documentation from the application code and handle the rendering part as well.

In this post, I am going to integrate Swagger 2 documentation into a Spring Boot based REST web service. So I am going to use Springfox implementation to generate the swagger documentation. If you want to know how to run/build Spring Boot project, please refer my previous post.

Springfox provides two dependencies to generate API Doc and Swagger UI. If you are not expecting to integrate Swagger UI into your API level, no need to add  Swagger UI dependency.

<dependency>
    <groupId>io.springfox</groupId>
    <artifac…

Building a RESTFul Service using Spring Boot

Image
Everyone is talking about Microservices such as WSO2 Microservice Framework, Spring Boot, etc. Since I haven't worked on any Spring related project since a very long time, I thought to implement a simple RESTFul service using Spring Boot.

So I started with Spring documentation. It is straightforward.  You can create the structure of your project using "Spring Initializr". This is an online tool where you can add all the desired dependencies to your project POM file. Since I am a big fan of Maven, I am generating a maven project.

In the Spring Initializr UI, you can choose the Language, Spring Boot Version, Project Group ID, artifact name, etc. Please refer below screenshot for information I have provided while generating the project.



When clicking on "Generate Project", it will download zipped maven project into your computer. Unzip it and import into an IDE. The initial project structure is like below.



In my HelloWorld REST service implementation, it accepts u…

Maven Compiler Plugin

The Maven Compiler Plugin is used to compile the java source code of your project. The default compiler is javac and is used to compile Java sources. By modifying pom.xml file, you can customize the default behavior of Maven Compiler Plugin. 

Using Maven Compiler Plugin, you can compile the source code of a certain project to a different version of JVM than what you are currently using. EX: compile using JDK 1.8 and target JVM is 1.7. Default source setting is JDK 1.5, and the default target setting is JDK 1.5

Example configuration is as below:


<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins…

Writing a Create API Executor for API.RXT

One of the use cases of the WSO2 Governance Registry is storing metadata information of different artifacts. In an Organization, there can be different metadata of different artifacts such their REST APIs, SOAP Services etc. In such a scenario you can use API and Service RXT which are available in the WSO2 Governance Registry to store metadata information.

With the use of API metadata which is stored in the WSO2 Governance Registry, you can publish APIs into WSO2 API Manager without accessing the web interface of the API Manager. This API creation is handled through lifecycle executor of the WSO2 Governance Registry. Once lifecycle state of the api publisher is reached, the executor will invoke Publisher REST API of the WSO2 API Manager and create the API. "Integrating with WSO2 API Manager" documentation explains about how to create an API using SOAP service meta data information.

If you want to create an API using the REST API metadata information available in the WSO2…

Maven excludes all transitive dependencies

"Transitive dependencies are a new feature in Maven 2.0. This allows you to avoid needing to discover and specify the libraries that your own dependencies require, and including them automatically."

I had a problem, where some dependencies were available in the run time, but they weren't available in the public nexus repositories. For an example, Hibernate depends on the Sun JTA API JAR and it is not available in the central Maven repository because it cannot be freely redistributed. So when building the project, it was trying to download transitive dependencies and got failed.

So I looked  a way to ignore all the transitive dependencies, and found that we can ignore all the associated dependencies of a given dependency. There we can exclude all transitive dependencies without specifying groupId and artifactId of the dependencies. So need to use astric(*) character as groupid and artifactid of the dependency.
<dependency> <groupId>sample.ProjectA</grou…

Build Maven Multi Module Project and Run using one Command

When we have maven multi module project, there will be dependency between sub project. Therefor if you need to build all projects need tp navigate to parent directory and run the mvn clean install command.

If we have web based multi module project, currently most people doing is go to parent directory and build the all projects using mvn install or mvn clean install command, after that navigate to web project and start web container.

Ex: Projects are P1(parent project), Core and Web. When we building project go to P1 and build the all subprojects, after that go to web project and start jetty server using mvn jetty:run

But that approach wasting you time little bit and when we doing same thing all the time it will become boring  task to do(build parent and go to child folder and run the server)

For avoid this issue I am going to show how to run web project with out navigate to child project.

following command will help you to run specific project without moving to sub project folder.

mv…

Change Jetty Server Running Port

Jetty is one one of the best development server which I have ever used. It is easy to work with jetty server and its light weight

Here I am going to show how to change jetty server running port.  Jetty server default running port is 8080.

Port with Maven: 

 Command line:          For starting jetty server we are using mvn jetty:run command. We can change the port jetty server running by passing run time argument to it.

mvn -Djetty.port=80 jetty:run


 Using pom.xml
                By changing jetty plugin system properties as below, you can change the jetty running port.
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <configuration> <stopPort>9966</stopPort> <stopKey>foo</stopKey> <systemProperties> <systemproperty> <name>jetty.port</name> <value>80</value> </systemproperty> </systemP…

Maven add jar without install

I have some problem with adding external jar in to maven project without install it. If I install JAR file other people who working on same project, have to install same JAR. them I want to avoid that issue.

First need to place jar file inside lib folder. After that using following POM entry we can use it as dependency.

<dependency> <groupId>com.chandana</groupId> <artifactId>example</artifactId> <version>0.1</version> <scope>system</scope> <systemPath>${basedir}/lib/example.0.1.jar</systemPath> </dependency>

Ex: <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.5.0</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency>
 Here very important option is <scope>system</scope>. It's say dependency provide with system, no need …

Maven commands and hint

svn checkout without subfolders

svn co --depth immediates URL
maven without internet(Without downloading)

mvn -o install
mvn --offline install

Force the check of the latest dependency jars
mvn -U install
mvn --update-snapshots install

*** -U option to update your snapshots

Skip the tests

mvn install -DskipTests

also use the maven.test.skip property to skip compiling the tests

mvn install -Dmaven.test.skip=true

Remote Debug

mvnDebug test
mvnDebug install


To avoid OutOfMemoryError

To avoid OutOfMemoryError when running maven, set environment variable MAVEN_OPTS as below

MAVEN_OPTS=-Xmx1024m -XX:MaxPermSize=256m


If you have issue with Maven does not find JUnit test cases to run:
By default Maven runs tests class which names start with Test or end with Test or TestCase.Do not messed up the naming convention

How to debug using Maven Jetty

Image
Debugging is a better approach to find out bugs in our programs. Here is a way to debug a maven project, and I am going to run my web application in the Jetty server.

First you need to add the following environmental variables on your Windows/Linux environment.

MAVEN_OPTS = -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=y Now go to your maven web project location through command line and start the jetter server -> mvn jetty:run



Now in eclipse debug configuration, add a remote Java application instance on port 4000. Then Add some debug points to your codes  and start the remote app instance on debug mode. You can see the steps from following screen shots.





In this manner you can debug your java web apps and find out the bugs. With this way you can debug your java web app.

if you are using maven latest version( Maven 2.0.8 or later)

run the
mvnDebug command instead of
mvn and attach a debugger on port 8000.

Love to hear from yo…

Spring-Hibernate Maven project- Part 3

Image
Hibernate Configuration and Project Deployment


In the previous post I created the hibernate mapping files for the Person-Address domain classes. In here we are going to create the hibernate configuration file which creates the database connection according to the defined URL, credentials and other properties.

On the deplyment of the project, the hibernate configuration file is executed through the commands in pom.xml.

We have to put the hibernate configuration file inside the src/main/resources directory. For that right cick on the directory, select new, select other. From there expand the hibernate icon and select Hibernate Configuration file (cfg.xml).

Click Next

Keep the default name "hibernate.cfg.xml" same and click Next
Give the values in the appeared window give the values as follows.

Make sure to verify your mysql username and password, and enter the corret one according to your mysql configuration.
hibernate.cfg.xml
<hibernate-configuration> <session-fa…

Spring-Hibernate Maven project- Part 2

Image
Hibernate Mapping

Here the two domain classes have one-to-many relationship. One Person can have many Addresses while one Adress can have only one person. So there is a one-to-many relationship between Person and Address.

In hibernate we have to create the mapping file for each domain class. The mapping file represents the database table which is mapped with the domain class. The table will be created according to things indicated inside mapping classes.

Mapping file for Person.java: - Person.hbm.xml
Mapping file for Address.java: - Address.hbm.xml

Mapping files are located inside src/main/resources. If you have configured Hibernate to be used inside Eclipse you can simply use it. Otherwise let's configure eclpse for hibrenate.

Hibernate-Eclipse Configuration 

Download Hibernate tools from here.
Extract the HibernateTools-3.X.zip file and put all the files inside the features folder into the features folder of the eclipse installation directory and put all the files inside the plu…

Spring-Hibernate Maven project- Part 1

Image
Here the maven project created in last post, will be extended to add new Persons in to system who has multiple Addresses.

Add the Spring Hibernate dependencies into pom.xml file inside project tags, after the version tag.
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.test.testapp</groupId> <artifactId>testapp</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.1.GA</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>${springVer…

Create a Simple Maven Project

Image
Open Eclipse.
Create a new maven project. File--New--Other
New Wizard will open. There go to maven icon and expand it. Then Select Maven Project item.




Click Next.
In the New Maven Project wizard, select the create a simple project (skip archetype selection) check box.



Click Next
In the New Maven Project wizard enter the following values.


Group Id: org.test.testapp
Artifact Id: testapp
Version: 0.0.1-SNAPSHOT
Packaging: jar