Wednesday 28 February 2018

Spring web service one

Spring MVC Framework and REST

1.Spring MVC :-Client request to Dispatcher-->handler mapping-->Controller-->view

2.REST spring  :-client request to web service in URI form--->intercepted by Dispatcher it find the controller based on incoming request -->request proced by controller and responds is return to the dispatcher which then dispatched to view

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.

##Using the @RestController Annotation

   Spring 4.0 introduced @RestController

    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

Friday 2 February 2018

Create Json From Java Code

to download eclipse https://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/neon3



Model or pojo

package test;

public class ProjectLIst {

String idProject;
String siteLocation;
String name;
String type;
String siteMangr;

public ProjectLIst(){}

public ProjectLIst(String idProject,String siteLocation,String name,String type,String siteMangr){
this.idProject=idProject;
this.siteLocation=siteLocation;
this.name=name;
this.type=type;
this.siteMangr=siteMangr;
}





public String getIdProject() {
return idProject;
}

public void setIdProject(String idProject) {
this.idProject = idProject;
}

public String getSiteLocation() {
return siteLocation;
}
public void setSiteLocation(String siteLocation) {
this.siteLocation = siteLocation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSiteMangr() {
return siteMangr;
}
public void setSiteMangr(String siteMangr) {
this.siteMangr = siteMangr;
}



}

##################################
package test;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;


public class JsonCreating {
public static void main(String args[]){
ProjectLIst p1=new ProjectLIst("1","kochi","lee","steel","vimal");
ProjectLIst p2=new ProjectLIst("1","kochi","lfsdee","sdfdteel","errvimal");
ProjectLIst p3=new ProjectLIst("1","kochiee","wewelee","stedfel","erfdfvimal");
List<ProjectLIst> pList=new ArrayList<ProjectLIst>();
pList.add(p1);
pList.add(p2);
pList.add(p3);
Gson gson = new Gson();
Type type = new TypeToken<List<ProjectLIst>>() {}.getType();
        //String json = gson.toJson(list, type);
String myjson=gson.toJson(pList,type);
System.out.println(myjson);
}

}


############################
Nested

package test;

import java.util.List;

public class ProjectLIst {

String idProject;
String siteLocation;
String name;
String type;
String siteMangr;

List<Phone> phons;

public ProjectLIst(){}

public ProjectLIst(String idProject,String siteLocation,String name,String type,String siteMangr,List<Phone> ph){
this.idProject=idProject;
this.siteLocation=siteLocation;
this.name=name;
this.type=type;
this.siteMangr=siteMangr;
this.phons=ph;
}





public List<Phone> getPhons() {
return phons;
}

public void setPhons(List<Phone> phons) {
this.phons = phons;
}

public String getIdProject() {
return idProject;
}

public void setIdProject(String idProject) {
this.idProject = idProject;
}

public String getSiteLocation() {
return siteLocation;
}
public void setSiteLocation(String siteLocation) {
this.siteLocation = siteLocation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSiteMangr() {
return siteMangr;
}
public void setSiteMangr(String siteMangr) {
this.siteMangr = siteMangr;
}



}

----
package test;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class JsonNested {
public static void main(String args[]){
  Phone ph1=new Phone();
  ph1.setPhnnum("33333333");
  Phone ph=new Phone();
  ph.setPhnnum("33333333");
  List<Phone> phonelist=new ArrayList<Phone>();
  phonelist.add(ph);
  phonelist.add(ph1);
ProjectLIst p1=new ProjectLIst("1","kochi","lee","steel","vimal",phonelist);
ProjectLIst p2=new ProjectLIst("1","kochi","lfsdee","sdfdteel","errvimal",phonelist);
ProjectLIst p3=new ProjectLIst("1","kochiee","wewelee","stedfel","erfdfvimal",phonelist);
List<ProjectLIst> pList=new ArrayList<ProjectLIst>();
pList.add(p1);
pList.add(p2);
pList.add(p3);
Gson gson = new Gson();
Type type = new TypeToken<List<ProjectLIst>>() {}.getType();
        //String json = gson.toJson(list, type);
String myjson=gson.toJson(pList,type);
System.out.println(myjson);
}

}
  output
[{"idProject":"1","siteLocation":"kochi","name":"lee","type":"steel","siteMangr":"vimal","phons":[{"phnnum":"33333333"},{"phnnum":"33333333"}]},{"idProject":"1","siteLocation":"kochi","name":"lfsdee","type":"sdfdteel","siteMangr":"errvimal","phons":[{"phnnum":"33333333"},{"phnnum":"33333333"}]},{"idProject":"1","siteLocation":"kochiee","name":"wewelee","type":"stedfel","siteMangr":"erfdfvimal","phons":[{"phnnum":"33333333"},{"phnnum":"33333333"}]}]