Saturday, January 27, 2018

Spring MVC Framework and REST

Understanding REST

Since Spring Framework is the most popular and the standard framework for developing Java web application and RESTful Web Services.

While the traditional MVC controller relies on the View technology, the RESTful web service controller simply returns the object and the object data is written directly to the HTTP response as JSON/XML.
REST (Representational State Transfer) was introduced and defined in 2000 by Roy . REST is an architectural style for designing distributed systems. It is not a standard but a set of constraints, such as being stateless, having a client/server relationship, and a uniform interface. REST is not strictly related to HTTP, but it is most commonly associated with it

Principles of REST

  • Resources expose easily understood directory structure URIs.
  • Representations transfer JSON or XML to represent data objects and attributes.
  • Messages use HTTP methods explicitly (for example, GET, POST, PUT, and DELETE).
  • Stateless interactions store no client context on the server between requests. State dependencies limit and restrict scalability. The client holds session state.

HTTP methods

Use HTTP methods to map CRUD (create, retrieve, update, delete) operations to HTTP requests.

  • GET     -    @RequestingMapping (method =  RequestMethod.GET)         or     @RequestingMapping ("/topics")
  • POST   -    @RequestingMapping (method =  RequestMethod.POST   ,  value = "/topics")
  • PUT     -    @RequestingMapping (method =  RequestMethod.PUT)
  • DELETE  -    @RequestingMapping (method =  RequestMethod.DELETE)
HTTP Verb CRUD Entire Collection (e.g. /customers) Specific Item (e.g. /customers/{id})
POST Create 201 (Created), 'Location' header with link to /customers/{id} containing new ID. 404 (Not Found), 409 (Conflict) if resource already exists..
GET Read 200 (OK), list of customers. Use pagination, sorting and filtering to navigate big lists. 200 (OK), single customer. 404 (Not Found), if ID not found or invalid.
PUT Update/Replace 405 (Method Not Allowed), unless you want to update/replace every resource in the entire collection. 200 (OK) or 204 (No Content). 404 (Not Found), if ID not found or invalid.
PATCH Update/Modify 405 (Method Not Allowed), unless you want to modify the collection itself. 200 (OK) or 204 (No Content). 404 (Not Found), if ID not found or invalid.
DELETE Delete 405 (Method Not Allowed), unless you want to delete the whole collection—not often desirable. 200 (OK). 404 (Not Found), if ID not found or invalid.

HTTP status codes

Status codes indicate the result of the HTTP request.
  • 1XX - informational
  • 2XX - success
  • 3XX - redirection
  • 4XX - client error
  • 5XX - server error

Using the @ResponseBody Annotation

When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the http response automatically. Each method in the Controller class must be annotated with @ResponseBody.

Behind the Scenes

Spring has a list of HttpMessageConverters registered in the background. The responsibility of the HTTPMessageConverter is to convert the request body to a specific class and back to the response body again, depending on a predefined mime type. Every time an issued request hits @ResponseBody, Spring loops through all registered HTTPMessageConverters seeking the first that fits the given mime type and class, and then uses it for the actual conversion.

Using the @RestController Annotation

Spring 4.0 introduced @RestController, a specialized version of the controller which is a convenience annotation that does nothing more than add the @Controller and @ResponseBody annotations. By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods. The @ResponseBody annotation is active by default.


References : –

Wednesday, January 24, 2018

Lessons learnt

# Find strong reason why are we doing something , develop discipline through singular activities. Pre planning for temptation . Use discipline got through doing single tasks well to do other tasks
#  Break down solutions that you goning to give ,check whether are they compatible each other.
#learn how to work fast .Try to understand things that wasted more time.Note down process that taken to early process, and revice why that actions taken.
#Ask yourself before doing anything , what should do to achieve task .break that into small task
#Develop your concentration , meditate.
#Before you talk with your boss , prepare well about what   are you talking about .
#You sholud learn new technologies and sholud apply them ,Best way is starting side project so you can apply what you have learnt.
#Most time answer for your problem is in internet , but you have to dive and find best answer.
#Challenge yourself , do what you are afraid to do.
#Don't depend on single income
#prove them they are wrong to people who said you are good enough.
#It is your duty to add worth to your life.
#We hope for brand new beginning always  ,without trying to make present situation  better. (One day arrived new beginning allso,will be bacome old)
#Learn the rules well.
#Some times there will be no time to do what you want ,after continuing procrastination.
#Its all about keep moving forward and fight back for your goals
#Don't lost your humanity .
#Before looking at chances that not yet came , try to get maximum of current opportunities you got.
#Focus on the process you need to take to achieve goal instead of focusing on goal.
#You should finish ,what you have started.
#Don't wait till motivation comes.It's not going to come ,you have to push hard yourself and be discipline  to your work.
#
This is easy work:
 complaining  pretending   blaming   judging  resenting   
 taking     shouting   expecting      waiting   doubting
This is hard work:
 inspiring  learning   teaching   trusting   empowering
 building   committing   giving   loving   leading"

Monday, January 1, 2018

MySql - Commands


  • create database employee;    
  • show databases;
  • use employee;
  • select database();
  • create table employeeDetailExample
  ( fName varchar(25) NOT NULL,
   lName varchar(25) NOT NULL,
                     state CHAR(2)  NOT NULL  DEFAULT "PA",
                     birth_date DATE NOT NULL ,
                     sex ENUM ('M','F') NOT NULL ,
                     empID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
                     lastWorkedDate TIMESTAMP,
                     money_owed FLOAT NULL
                   );

  •   show tables;
  •   create table employeeDetail
                     (
                   fName varchar(25) NOT NULL,
                   state CHAR(2)  NOT NULL  DEFAULT "PA",
                   sex ENUM ('M','F') NOT NULL ,
                   empID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
                   lastWorkedDate TIMESTAMP
                   );
  •    drop table employeeDetail ;
  •    quit
  •    desc employeeDetail;
  •    INSERT INTO employeeDetail VALUES('ROSHAN','AC','M',NULL,2222333444);
  •    INSERT INTO employeeDetail  VALUES(fName,lName ) values('roshan','sanjeewa');

  Resource Url :- https://www.youtube.com/watch?annotation_id=annotation_103363&feature=iv&src_vid=Em-AhbqVe48&v=9ywkQ9aGhVo

Problems Occured - With Solutions Used

  •  When :- Creating db connection 
    • Error got :- Mon Jan 01 16:39:51 IST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended.According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set.
      For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'.
      You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.    
    • Solution used :-    Connection myConnection  = DriverManager.getConnection(dbUrl+ "?useSSL=false", userName, password);    used instead  of    Connection myConnection  = DriverManager.getConnection(dbUrl, userName, password);
    • Help got from :- https://stackoverflow.com/questions/34189756/warning-about-ssl-connection-when-connecting-to-mysql-database
  • When :- Error while loading class org.springframework.http.server.reactive.ServletServerHttpRequest$RequestAsyncListener   Caused by: java.lang.NoClassDefFoundError:org/springframework/http/server/reactive/ServletServerHttpRequest$RequestBodyPublisher\
    • Solution used :-  Added following dependency 
    • <dependency>
    •    <groupId>org.reactivestreams</groupId>
         <artifactId>reactive-streams</artifactId>
         <version>1.0.1</version>
      </dependency>
  •   When :- Trying to run a .jar file 
  • Problem : - Unable  to  run  jar file 

The AI Driven Software Developer, Optimize Innovate Transform

  The AI-Driven Software Developer: Optimize, Innovate, Transform": AI Transformation in Software Development : Understand how AI is re...