Sharing my learnings....

Tag: Java

Send SMS in NodeJS using Nexmo

Hi, As I’m a JS enthusiast; I love doing JavaScript development due to its easiness & lot of people there to help.

Today, I have come up to show you how to implement, message sending facility using NodeJS using Nexmo service provider.

A lot of people are interested in start-ups & need to grow their business using SMS promotions.

Let’s move to the coding portion.

First of all, you have to create Nexmo account: Click Here

Now, create a new folder with a name you want

Create package.json using

npm init

Now,

We have to install express & body-parser to the application to use.

npm install express body-parser — save

Then, create an index.js file in the root folder

const express = require('express');
//importing express to useconst

bodyParser = require('body-parser');
//importing body parser to get the body input const

app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

require('./controller.js')(app);

//requiring app Which is an express instance to use in controller file

const server = app.listen(3000);
//configure the server to run on port 3000

console.log("Server working on 3000")

Now, create the controller.js file in the root folder.

module.exports = function (app) {
	const Nexmo = require('nexmo');
	const nexmo = new Nexmo({
	        apiKey: Your_API_KEY,
	        apiSecret: Your_API_SECRET_KEY
	});

	const config = {
		number: YOUR_REGISTERED_MOBILE_NUMBER
	}

	app.post('/send', (req, res) => {
	//Setting endpoint of /send
	// Send SMS
	     nexmo.message.sendSms(
	     config.number,
	     req.body.toNumber,
	     req.body.message, { type: 'unicode'},
     (err, responseData) => { if (responseData)
     {
	     console.log(responseData)
     }
	});
});
}

Here we are using the app instance which we declared in the index.js.

Then, we are importing the Nexmo module & create an instance of the Nexmo module;

for apiKey & apiSecret you have to give your Nexmo account details.

In the config object, you have to put the registered mobile number for your Nexmo account as a value for the number attribute.

And, after that, a POST request with a “/send” endpoint.

Wrapping the nexmo.message.sendSms() method which is predefined in the Nexmo to send message inside the POST request.

Finally, we are console the message details in the terminal/command line.

Now, Start the server using

node index.js

And test it with Postman.

If you get Non White-listed Destination — rejected error.

You have to register it in your Nexmo Account.

To do that Click Here

Yes, you have done it.

Happy Coding Folks..!!

Annotation & their uses in Java

Hello, I’m newbie to Java world & so new to Spring Boot. So, I don’t have a prior experience with spring also. As a new one, I’m writing this to new people to Java. Experts, correct me in the comments if its wrong.

What is Annotation..?

a note by way of explanation or comment added to a text or diagram.

Above, is the dictionary explanation for Annotation.

In Java

Annotation is a tag that used for

  1. methods
  2. class
  3. interface

which adds more information about those for Java compiler / JVM(Java Virtual Machine).

You may have already experienced with some built-in Java function when you done with basic Java stuffs.

Like,

  1. @override — used to override the parent class method in sub class method
  2. @SuppressWarnings — used to suppress warnings issued by the compiler.
  3. @Deprecated — compiler prints warning because of the method is deprecated as it could be removed in the future versions. So that its better not to use such method.

Let’s have a look at Java Custom Annotation

Java Custom Annotation or Java User defined annotations are easy to use and create (That means even you and me can create an annotation according to our need).

@interface element is used to declare an annotation.

Like this

@interface parathan{}

to create annotation; annotation should have the following characters

  • method should not have parameters
  • should not throw any clause
  • may have default values
  • should return a class, method, enum, String like primitive data type

Type of Annotation

  1. Marker Annotation
  2. Single Value Annotation
  3. Multi-value Annotation
  4. Marker Annotation — Annotation that have no methods inside it.

Eg: @Deprecated @Override

@interface parathan{}

  1. Single-value Annotation — Annotation that have one method

@interface parathan{

int value();

}

Default value can be provided for it by following code snippet

@interface parathan{

int value() default 0;

}

Applying a Single Annotation in code

@parathan(value=10)

3.Multi-value Annotation — Annotation that has more method than one

@interface parathan{

int age();

String name();

String country();

}

Applying Multi Annotation can be as follows

@parathan(age=20,name=”Parathan Thiyagalingam”,country=”Sri Lanka”)

Built in Annotations used in Custom Annotations

  1. @Target
  2. @Retention
  3. @Inherited
  4. @Documented

1. @Target —

used to betoken to which type the annotation to be used

For that we have to import java.lang.annotation.ElementType

we use

@Target(ElementType.some_thing)

Here some_thing need to be replaced by the following keywords if you are using where the annotation need to be applied.

If you are going to use it for “class,interface,enumeration”

then

@Target(ElementType.Type)

If you are going to use it for methods

then

@Target(ElementType.METHOD)

Credit: Javatpoint.com

Eg to use for class and method is follow

@Target({ElementType.Type,ElementType.METHOD})

@interface parathan{

int age();

String name();

}

2. @Retention

Used to betoken for what level the annotation need to be available.

There are 3 level

  • SOURCE — this refers the source code, which the annotation will not be available in compiled class.
  • CLASS — this refers to the .class file, which the annotation will not available to JVM but available for java compiler. So, it will be in the class file.
  • RUNTIME — refers runtime, which is available to bothe java compiler & jvm.

Eg:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@interface parathan{

int age();

String name();

}

The above snippet represents the annotation will be available at the run time & the annotation is targeted to the class.

3. @Inherited :

Normally the annotations are not inherited to sub classes to inherit the annotation to sub class @Inherited is used.

@Inherited

@interface parathan{ } //Now it will be available to subclass also

@interface parathan{ }

class MainClass{}

class Subclass extends MainClass{} //sub class extends the super class

As the annotation “parathan” is inherited;

the MainClass uses the annotation as the SubClass extends the MainClass,

therefore, the SubClass can also access the annotation

4.@Documented

It is used to include the annotation in the Java documentation.

The annotation I’m writing here, is because I’m posting REST API in Java using Spring Boot & MySQL as Parts.

So, there we are using a lot of annotations to make our tasks easy.

If I made any mistakes.. Please comment below. Or share with your friends.

Happy Coding Folks…!!

Split & Map a String in Java

Hi, everyone. I’m learning the basic concepts of Java since last month. So, while I’m learning I did some example by my own. In future post I’m planning to publish those code with you about what I learnt & how I did it.

Here, I want to show you how I split & mapped a string which in the following format.

key1=value1;key2=value2;key3=value3

This is my code

import java.util.*;

public class App {

    public static void main (String args[]) {

        Map<String, String> map = new HashMap<String, String>();
        String gotData = "key1=value1;key2=value2;key3=value3";

        if(gotData == **""**){
        	map = null;
	} else {

   		for(String keyValue : gotData.split(";")) {
        		String[] key = keyValue.split("=");
        		map.put(key[0], key[1]);
    		}
	}
        Map<String,String> finalOutput= map;

        System.**_out_**.println(finalOutput);

    }
}

Could you get it out…??

I’m explain it according to my knowledge…

I imported all java.util package modules to use “Map, Hash Map, put” keyword which are belong to the util package.

Then in the App class I declared a map of datatype Map.

For loop is used to loop until the string fully read by the program.

Inside that if else is used to avoid the “ArrayBoundExcaption” which is very common exception when using Map, Arrays in Java.

Splitting the string by “ ; ” and “ = ” and assigning the key to the o th index of Map & value to the 1st index of Map

This is what I got as the output

{key1=value1, key2=value2, key3=value3}

Screenshot of What I got as Output using Debugger mode

for o (zero(th Index

value1 assigned to key1

value2 assigned to key2

value3 assigned to key3

If you have another simple code Share it in the comment.

Thanks,