Browse Month: January 2017

What is web service and what are the different types of web services?

This tutorial we are going to learn web service and what are the different types of web services.

Web Service:

Web Services is a technology communicate between two electronic devices over the internet or intranet using the collection of standard protocols for world wide web (WWW), service data typically exchange XML or JSON format.

A standard Web Service using below components:

  • XML (eXtensible Markup Language)
  • SOAP (Simple Object Access Protocol)
  • UDDI (Universal Description, Discovery, and Integration)
  • WSDL (Web Services Description Language)

XML stands for eXtensible Markup Language. A uniform data representation and exchange mechanism that defines a set of rules for encoding documents in a format that is both human and machine readable.

SOAP stands for Simple Object Access Protocol. SOAP is a HTTP based protocol specification for exchanging structured information in XML form, interact across any computer networks, specifically HTTP (Hypertext Transfer Protocol).

UDDI stands for Universal Description, Discovery, and Integration. UDDI is a platform independent, UDDI is an XML based network protocol, where web service registers their web services and the client can search, find and invoke web services. SOAP is the most popular protocol for communication.

WSDL stands for Web Services Description Language, WSDL is written in XML format and it is used a standard metalanguage to describe the web services offered, WSDL is a program/language that describes the provider services as a set of endpoints messages containing either document-oriented or procedure-oriented information. The same web service can have multiple endpoints using different protocols and what communications protocol is used to talk to that service.

Web Services Types:

  1. SOAP (Simple Object Access Protocol)
  2. REST (Representational State Transfer)

SOAP stands for Simple Object Access Protocol. SOAP is an HTTP based protocol specification for exchanging structured information in XML form, interact across any computer networks, specifically HTTP (Hypertext Transfer Protocol).

REST stands for Representational State Transfer. REST is architectural principles for building web service using HTTP protocol using basic HTTP methods like POST, GET, PUT and DELETE (CRUD – Create, Read, Update, and Delete).

REST web service supports formats like JSON, XML, HTML and Text

  • REST uses standard methods like

What are differences between SOAP web services and REST web services?

SOAP and REST web service cannot be compared directly, see below differences.

S.No SOAP REST
1 SOAP stands for Simple Object Access Protocol. REST stands for Representational State Transfer.
2 SOAP is a protocol REST is an architectural style
3 SOAP can use almost any transport request REST uses HTTP and HTTPS
4 SOAP supports state full calls REST is totally stateless operations
5 SOAP is a more strict contract in the form of WSDL REST does not have any strict contract
SOAP defines own security REST will inherit the transport level security over HTTP.
6 SOAP response cannot be cached REST response can be cached
7 SOAP format will be restricted to XML REST will support formats like XML, JSON, HTML, Text
8 Java API for SOAP Web Services, JAX-WS Java API for REST Web Services, JAX-RS
9 SOAP requires an XML wrapper for all the request and response REST is lighter, this reason REST is prepared for mobile devices
10 SOAP bit slower and less prepared REST is faster compared to SOAP, more prepared, simple to use and lightweight

 

What is Log4J?

This tutorial is going to explain about Apache Log4J.

  • Apache Log4J is logging framework provided by Apache Software Foundation.
  • Log4j is a Java logging framework used in Java programming and Java applications to debug any error/exception in the logs.
  • Log4j has various built-in types of logging debug levels, see below low to high order.

Log4J Logging Levels:

Logging Levels Description
OFF The highest possible rank and is intended to turn off logging.

 

FATAL Severe errors that cause premature termination to lead the application to abort.
ERROR Error will allow the application to continue running (unexpected conditions/ runtime errors).

 

WARN Runtime situation that is undesirable or unexpected, almost error condition.
INFO Info will be immediately visible to console.
DEBUG Detailed information on the flow through the system, all are written to logs, this level is very useful to debug the program/application.

 

TRACE More detailed information and those are written to logs, more than debug level.
ALL All logging levels, including custom levels.

 

 Step 1

In Eclipse create a Java Project (File > New > Java Project)

Step 2

1 . Create a class file > java

/**

*/

package com.rrmaha.test;

import org.apache.log4j.Logger;

/**

* @author rrmaha.com

*/

public class LogTest {

static final Logger logger = Logger.getRootLogger();

public static void main(String[] args) {

logger.debug(“Test debug message”);

logger.info(“Test info message”);

logger.warn(“Test warn message”);

logger.error(“Test error message”);

logger.fatal(“Test fatal message”);

}

}

2. Create a file properties file > properties

# Define the root logger with appender file

log = /usr/home/log4j

log4j.rootLogger = debug, FILE

 

# Define the file appender

log4j.appender.FILE=org.apache.log4j.FileAppender

log4j.appender.FILE.File=${log}/log.out

 

# number of log files to keep before deleting the oldest one

log4j.appender.MaxBackupIndex=25

log4j.appender.MaxFileSize=1KB

log4j.appender=org.apache.log4j.RollingFileAppender

log4j.appender.file=jpe.log

 

# Define the layout for file appender

# Log message layout: date-time [thread] priority category – message lineTerminator

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.conversionPattern=%d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c – %m%n

 

3. Create a folder

/usr/home/log4j

4. Create output file name(log file name)

log.out

5. Add log4j jar file > log4j-1.2.16.jar

Step 3

Step 4

Open a file > \usr\home\log4j\ log.out

04 May 2017 19:59:27 [main] DEBUG root – Test debug message

04 May 2017 19:59:27 [main] INFO  root – Test info message

04 May 2017 19:59:27 [main] WARN  root – Test warn message

04 May 2017 19:59:27 [main] ERROR root – Test error message

04 May 2017 19:59:27 [main] FATAL root – Test fatal message