spring boot validation

spring boot validation

Spring boot validation and Custom Validations

Spring boot validation is probably one of the cool features spring has to provide. Not only spring provided validations but also, it allows custom validation as well.

Custom Spring boot validator will allow you to act like the native validator in your application. Not only that, once you create custom spring validator, you can reuse it.

What is spring validator

Spring validator allows validating the data of the request. The validator will handle basic logic of handling validation than re-inventing the wheel.
Continue reading spring boot validation

important vim commands

important VIM commands

Important VIM commands for daily usage

Important VIM commands is selected vim commands that every programmer shall have by heart.

By far important vim commands might be those we use in everyday programming life. But there more to it.

What is vim

Vim is a smart text editor for linux distros. It allows plugs ins for different usages. Basically, it pretty much knows most of programming languages you open with it.

Important vim commands will allow you to have a fast and productive usage of VIM while working on it.

Why should I use vim

You should use vim because it is cool. Really it is cool. Apart from being cool, it is light weight and almost available in most linux distributions.

Vim allows having third party plugins that will allow you to do multiple tasks from coloring to memory based copy and more.

Vim is programming language friendly. If you are working on java, it will highlight the keywords for you. Same for PHP or javascript or almost for lots of languages.

how it install vim

Usually VIM will come with most of the linux flavors:

If you are on Ubuntu installing vim on ubuntu will be
Continue reading important VIM commands

git sub directory

how to add subdirectories in git

Adding subdirectories in existing git repository

You have done git push but the new folders didn’t got pushed. What happened?

Having new folders and subfolders in the git repository, but the files under those not showing in the server?

Adding subdirectories in existing git repositories should have been direct right? But, sometimes it is not. I will show you How to add subdirectory in existing git repository

Let’s say you have existing git repository my-git-repo and you want to add new folder under it: new-folder/another-sub-folder

Step by steps to add git subdirectories in existing git repository

1. Create folder in the repository you are going to create new folders

mkdir -p new-folder/another-sub-folder

2. Now if you do git status you will see the new folder under new listing.

3. Create a file under another-sub-folder otherwise git won’t show nothing.

cd new-folder/another-sub-folder && touch some-file.xml

4. Go to another-sub-folder and issue git init

5. Now go back to the root of the folder and issue git add new-folder/

6. You are done, when you do git status you will see your new files being listed for commit.

Group by week mysql

How to group by week on mysql

Group by week in mysql is common specially for reporting. Here is how to do it.

If the column is with format yyyy-mm-dd


SELECT COUNT(*), YEARWEEK(__DATE_COLUMN__)
FROM __TABLE__ 
WHERE (__DATE_COLUMN_) > '2018-03-01 00:00:00'
GROUP BY YEARWEEK(__DATE_COLUMN__)

If the column is unixtime stamp format


SELECT COUNT(*), YEARWEEK(FROM_UNIXTIME(__DATE_COLUMN__))
FROM __TABLE__ 
WHERE FROM_UNIXTIME(__DATE_COLUMN__) > '2018-03-01 00:00:00'
GROUP BY YEARWEEK(FROM_UNIXTIME(__DATE_COLUMN__))

Where the __TABLE__ is he mysql table and __DATE_COLUMN__ is the column you are interested.

facebook logo

Facebook Link Ownership in Facebook posts

Facebook link ownership when posting links from using facebook app

Facebook link ownership is hot issue now. Facebook is getting smarter and stricter from time to time.

In good days, all you have to do was to post the link to the page automatically using the app and get the traffic.

Now, when you try to do that, Facebook requires you to verify your ownership of the website you are posting a link to.

Part of it was done through the Open Graph tags. But now, it requires a bit more than that.

How to set a link ownership of the website for facebook posts

Before we dive into the how, you need to make sure you qualify the following points.

1. The first thing is, you need to have access to the website you are trying to post links from facebook app yourself.

2. You are an admin of the facebook page you are trying to post.

3. It is assumed you already have facebook app you want you use for posting to facebook page.

Now here are the steps:

1. Go to business.facebook.com

2. Click the business setting, you will find it on the right top corner.

3. On the left, under the account menu select pages and click on the add to add the page.

4. Select “Add Page” and follow the wizard to add the page.

5. Once you are done adding the page, on the left menu under brand safety, click on domains.

6. Here add click on “Add” to add the the website. You need to have access to the server of the website as it gives you file to put there.

7. Once you are done with number 6 and do the verification, go back to domains and select the website you verified.

8. Here you see options to assign pages. When you click on “Assign Pages” you get the list of pages you added on the previous steps. Now you can associate the page to the website.

Congratulations! Now when you are using your Facebook app to post articles or anything from your website to your Facebook page, Facebook knows you owned it and will allow you to post.

Good luck.

Redirect http url to https

Force redirect http to https using htaccess

Force redirect http traffic to https

Redirect http to https url using simple file. If you have recently switched to https version, which is recommended, then you want to force the traffic to https.

This can be accomplished using .htaccess file.

How to redirect https to http

1. Create/update the .htaccess file on your root website.
2. Add the following line on the top part of the .htaccess file


RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

3. That is it. You don’t need to restart the server for this case.

You can refer this page for more htaccess examples

You don’t have permission to access / on this server. Apache error

Apache refusing to serve with you don’t have permission to access error

Here are things to fix the problem

Have your config in your apache config like this:


       ServerName someWebname.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        
                Options All
                AllowOverride All
                require all granted
        

1. Make sure the directory you are accessing has at least 755 permission mode
2. Make sure to restart the server after you change the configs
3. Check if you have .htaccess file with different setting
4. In some cases, make sure the directory is accessible by apache – www-data.

Apache Not Service Files from Icons Folder

My ico, png, jpg and more files from Apache are not being served

It is a bit crazy but the icons folder that is residing in the root folder is reserved. Well kind of.

If you have icons folder in the root, then your files under that folder might have hard time being served.

What is the fix

The easiest is to rename your “icons” folder. If you have admin access to the apache config files, on one of the conf files, search for
Alias /icons/ .. and change it accordingly.

Create Tables From Entity Definitions From JPA Spring

How to create database tables from JPA using Hibernate

We know ORM. It will be a nightmare or a bliss based on the angle you are looking at it. This time a bliss :)

If you have started development from the java side and you have all the entities figured out, then hibernate can create the database for you from the entities.

For this example I will be assuming you are using Spring and Hibernate, with that, how do you create the database from the entities

Initial Setup assumptions

So, I will be assuming you have the spring being setup already, the configuration file for your application would look like:


jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/database
jdbc.username = root
jdbc.password = 123
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = create|create-drop|update|none

Here the values to hibernate.hbm2ddl.auto could be create – if you want to crate during the sessionFactory initiation, create-drop, update when you want to update the exiting database when there is a change on the schema or none if you don’t want to do anything with it.

java class not found exception

spring error ClassNotFoundException org.springframework.web.context.ContextLoaderList

ClassNotFoundException org.springframework.web.context.ContextLoaderList

When working on spring framework, you might get this error specially on eclipse. I am assuming you are working on maven and eclipse environment.

The ClassNotFound exception is a bit self explanatory that eclipse could not find the mentioned class ContextLoaderList

The web.xml file for the spring configuration might look like


<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/application-context.xml</param-value>
 </context-param>

 <listener>
   <listener-class>
         org.springframework.web.context.ContextLoaderListener
   </listener-class>
 </listener>

With this, you might have similar file like application-context in the WEB-INF path

How to fix ClassNotFoundException for ContextLoaderList

The problem will during deployment. It is not basically compile time error. It is rather runtime error.

So, we have to make sure the jar responsible for this class is available in the classpath that eclipse is reading.

Pom dependency check

Make sure you have the same/similar configuration as the one listed below on your pom file.


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

You can have the right version for your spring in the parameter section or you can just replace it with the current version you have.

Using Deployment Assembly fix in eclipse

As we know, it is a deployment time problem, hence we have to tell the assembly where it can get its dependencies.

      Right click on the project
      Go to/search for Deployment Assembly
      Click on Add
      Select on Java Build path entries
      Select all the maven dependencies
      Finish
      Clean and build the project

Either of the above methods should fix the problem