Webservices4PM 150320211
Webservices4PM 150320211
Webservices# 4:30PM
Mr. RAGHU
------------------------------------
email: javabyraghu@gmail.com
fb: https://www.facebook.com/groups/thejavatemple
JSON - JACKSON
key: {
}
}
------------------[jackson-databind Annotations]-------------------
a. JSON Ignore: [ @JsonIgnore ]
To avoid any variable(property) using in JSON-JACKSON programming
we should ignore such variable.
============code====================
1. Model
package in.nit.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
public User() {
super();
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public String getUrole() {
return urole;
}
public void setUrole(String urole) {
this.urole = urole;
}
2. Test class
package in.nit.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import in.nit.model.User;
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
-----------------------------------------------------------------
*) JSON to File:-
ObjectMapper(C) has provided method name:
writeValue(file,object):void
*) Test class:
package in.nit.test;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import in.nit.model.User;
=> Here we need to pass JSON as String and class which you need
its object that conatins JSON data.
--Reflection --
Class<Employee> cls = Employee.class;
Class<Employee> cls = Class.forName("in.nit.model.Employee");
---------------
public Employee() {
super();
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getEsal() {
return esal;
}
public void setEsal(double esal) {
this.esal = esal;
}
@Override
public String toString() {
return "Employee [eid=" + eid + ", ename=" + ename + ", esal=" + esal +
"]";
}
2. Test class
package in.nit.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import in.nit.model.Employee;
System.out.println(emp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
=======Ex#2 Code============================
1. Models
package in.nit.model;
}
--
package in.nit.model;
//ctrl+shift+O (imports)
import java.util.List;
import java.util.Map;
public Employee() {
super();
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getEsal() {
return esal;
}
public void setEsal(double esal) {
this.esal = esal;
}
public List<String> getPrjs() {
return prjs;
}
public void setPrjs(List<String> prjs) {
this.prjs = prjs;
}
public Map<String, Double> getVers() {
return vers;
}
public void setVers(Map<String, Double> vers) {
this.vers = vers;
}
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
this.addr = addr;
}
@Override
public String toString() {
return "Employee [eid=" + eid + ", ename=" + ename + ", esal=" + esal +
", prjs=" + prjs + ", vers=" + vers
+ ", addr=" + addr + "]";
}
2. Test class
package in.nit.test;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import in.nit.model.Employee;