Showing posts with label pojo. Show all posts
Showing posts with label pojo. Show all posts

August 11, 2018

Serializing POJO to JSON - Input parameterization

How to output a json like below using java program



{"id":"0001","name":"Cake","topping":[{"id":"5001","type":"None"},{"id":"5004","type":"Maple"}],"ppu":"0.55","type":"donut","batters":{"batter":[{"id":"1004","type":"Devil's Food"},{"id":"0001","type":"Regular"}]}}

Program

/**
 * 
 */
package com.belazy.pojostructure;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * @author consumerfed IT Section 
 * Kozhikode
 *
 */
public class ManiClass {

public static void main(String[] args) throws JsonProcessingException {
Javabelazy user = new Javabelazy();
user.setId("0001");
    user.setType("donut");
    user.setName("Cake");
    user.setPpu("0.55");
    Batter batter1 = new Batter();
    batter1.setId("0001");
    batter1.setType("Regular");
    Batter batter2 = new Batter();
    batter2.setId("1004");
    batter2.setType("Devil's Food");
    Batters batters = new Batters();
    Batter[] batter = new Batter[2];
    batter[1] = batter1;
    batter[0] = batter2;
batters.setBatter(batter );
    user.setBatters(batters); 
    Topping topping1 = new Topping();
    topping1.setId("5001");
    topping1.setType("None");
    Topping topping2 = new Topping();
    topping2.setId("5004");
    topping2.setType("Maple");
    Topping[] topping = new Topping[2];
    topping[0] = topping1;
    topping[1] = topping2;
user.setTopping(topping );
ObjectMapper objectmapper = new ObjectMapper();
    String json = objectmapper.writeValueAsString(user);
    System.out.println(json);
}

}


User class

package com.belazy.pojostructure;
public class Javabelazy
{
    private String id;

    private String name;

    private Topping[] topping;

    private String ppu;

    private String type;

    private Batters batters;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public Topping[] getTopping ()
    {
        return topping;
    }

    public void setTopping (Topping[] topping)
    {
        this.topping = topping;
    }

    public String getPpu ()
    {
        return ppu;
    }

    public void setPpu (String ppu)
    {
        this.ppu = ppu;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    public Batters getBatters ()
    {
        return batters;
    }

    public void setBatters (Batters batters)
    {
        this.batters = batters;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", name = "+name+", topping = "+topping+", ppu = "+ppu+", type = "+type+", batters = "+batters+"]";
    }
}

Topping class

package com.belazy.pojostructure;
public class Topping
{
    private String id;

    private String type;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", type = "+type+"]";
    }
}


Batters class
package com.belazy.pojostructure;
public class Batters
{
    private Batter[] batter;

    public Batter[] getBatter ()
    {
        return batter;
    }

    public void setBatter (Batter[] batter)
    {
        this.batter = batter;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [batter = "+batter+"]";
    }
}

Batter class

package com.belazy.pojostructure;
public class Batter
{
    private String id;

    private String type;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", type = "+type+"]";
    }
}


Output



September 05, 2017

How to copy one object from another in Java

Converting one object into another in java


Main class


import org.dozer.DozerBeanMapper; /** * @author belazy * */ public class MainClass { public static void main(String[] args) { DozerBeanMapper mapper = new DozerBeanMapper(); mapper.setMappingFiles(Arrays.asList("dozer_mapping.xml")); Employee employee = new Employee("Sachin", "tendulkar", 45); EmployeeNew employeeNew = mapper.map(employee, EmployeeNew.class); System.out.println(employeeNew.getEmployeeFirstName()); } }

EmployeeTest


/** * */ package com.belazy.dozer; /** * @author belazy * */ public class EmployeeTest { private String firstName; private String lastName; private int age; public EmployeeTest(String firstName, String lastName, int age) { // TODO Auto-generated constructor stub this.firstName = firstName; this.lastName = lastName; this.age = age; } /** * @return the firstName */ public String getFirstName() { return firstName; } /** * @param firstName the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * @return the lastName */ public String getLastName() { return lastName; } /** * @param lastName the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } }

EmployeeNew class

/** * */ package com.belazy.dozer; /** * @author belazy * */ public class EmployeeNew { private String employeeFirstName; private String employeeLastName; private int employeeAge; public EmployeeNew() { // TODO Auto-generated constructor stub } public EmployeeNew(String employeeFirstName,String employeeLastName, int employeeAge ) { // TODO Auto-generated constructor stub this.employeeAge = employeeAge; this.employeeLastName = employeeLastName; this.employeeFirstName = employeeFirstName; } /** * @return the employeeFirstName */ public String getEmployeeFirstName() { return employeeFirstName; } /** * @param employeeFirstName the employeeFirstName to set */ public void setEmployeeFirstName(String employeeFirstName) { this.employeeFirstName = employeeFirstName; } /** * @return the employeeLastName */ public String getEmployeeLastName() { return employeeLastName; } /** * @param employeeLastName the employeeLastName to set */ public void setEmployeeLastName(String employeeLastName) { this.employeeLastName = employeeLastName; } /** * @return the employeeAge */ public int getEmployeeAge() { return employeeAge; } /** * @param employeeAge the employeeAge to set */ public void setEmployeeAge(int employeeAge) { this.employeeAge = employeeAge; } }

Dozer mapping. xml

<mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://dozer.sourceforge.net" xsi:schemalocation="http://dozer.sourceforge.net
      http://dozer.sourceforge.net/schema/beanmapping.xsd">
    <mapping>
        <class-a>com.belazy.dozer.EmployeeTest</class-a>
        <class-b>com.belazy.dozer.EmployeeNew</class-b>
        <field>
            <a href="https://www.blogger.com/null">firstName</a>
            <b>employeeFirstName</b>
        </field>
        <field>
            <a href="https://www.blogger.com/null">lastName</a>
            <b>employeeLastName</b>
        </field>
        <field>
            <a href="https://www.blogger.com/null">age</a>
            <b>employeeAge</b>
        </field>
    </mapping>
</mappings>




http://www.java2s.com/Open-Source/Java_Free_Code/Tutorial/Download_travelport_uapi_tutorial_Free_Java_Code.htm

htm
http://testws.galileo.com/GWSSample/Help/GWSHelp/java_sample_step_2__booking_an_air_reservation.htm

https://support.travelport.com/webhelp/GWS/Content/Overview/Getting_Started/Sample_Web_Service_Calls/Java_Sample_Step_1__Air_Availability_Request.htm


http://www.galileo.co.kr/document/Galileo%20Web%20Services%20Product%20Overview.pdf

August 26, 2017

A Simple Java Program to Copy one object to Another

Copying one object to another without using getters and setters


Employee POJO Class


/**
 * Employee POJO class
 */
package com.belazy.dozer;

/**
 * @author sachin
 *
 */
public class Employee {

private String employeeFirstName;
private String employeeLastName;
private int employeeAge;

public Employee(String employeeFirstName,String employeeLastName, int employeeAge ) {
// TODO Auto-generated constructor stub
this.employeeAge = employeeAge;
this.employeeLastName = employeeLastName;
this.employeeFirstName = employeeFirstName;
}
/**
* @return the employeeFirstName
*/
public String getEmployeeFirstName() {
return employeeFirstName;
}
/**
* @param employeeFirstName the employeeFirstName to set
*/
public void setEmployeeFirstName(String employeeFirstName) {
this.employeeFirstName = employeeFirstName;
}
/**
* @return the employeeLastName
*/
public String getEmployeeLastName() {
return employeeLastName;
}
/**
* @param employeeLastName the employeeLastName to set
*/
public void setEmployeeLastName(String employeeLastName) {
this.employeeLastName = employeeLastName;
}
/**
* @return the employeeAge
*/
public int getEmployeeAge() {
return employeeAge;
}
/**
* @param employeeAge the employeeAge to set
*/
public void setEmployeeAge(int employeeAge) {
this.employeeAge = employeeAge;
}



}


Employee New POJO Class ( The subclass of employee )

/**
 *  Employee New class ( The subclass of employee )
 */
package com.belazy.dozer;

/**
 * @author virat
 *
 */
public class EmployeeNew {
private String employeeFirstName;
private String employeeLastName;
private int employeeAge;
public EmployeeNew() {
// TODO Auto-generated constructor stub
}
public EmployeeNew(String employeeFirstName,String employeeLastName, int employeeAge ) {
// TODO Auto-generated constructor stub
this.employeeAge = employeeAge;
this.employeeLastName = employeeLastName;
this.employeeFirstName = employeeFirstName;
}
/**
* @return the employeeFirstName
*/
public String getEmployeeFirstName() {
return employeeFirstName;
}
/**
* @param employeeFirstName the employeeFirstName to set
*/
public void setEmployeeFirstName(String employeeFirstName) {
this.employeeFirstName = employeeFirstName;
}
/**
* @return the employeeLastName
*/
public String getEmployeeLastName() {
return employeeLastName;
}
/**
* @param employeeLastName the employeeLastName to set
*/
public void setEmployeeLastName(String employeeLastName) {
this.employeeLastName = employeeLastName;
}
/**
* @return the employeeAge
*/
public int getEmployeeAge() {
return employeeAge;
}
/**
* @param employeeAge the employeeAge to set
*/
public void setEmployeeAge(int employeeAge) {
this.employeeAge = employeeAge;
}

}

Main class


Main Java Class

/**
 * 
 */
package com.belazy.dozer;

import org.dozer.DozerBeanMapper;

/**
 * @author jadeja
 *
 */
public class MainClass {
public static void main(String[] args) {
DozerBeanMapper mapper = new DozerBeanMapper();
Employee employee = new Employee("Sachin", "tendulkar", 45);
EmployeeNew employeeNew = mapper.map(employee, EmployeeNew.class);
System.out.println(employeeNew.getEmployeeFirstName());
}

}


Output


10 [main] INFO org.dozer.config.GlobalSettings - Trying to find Dozer configuration file: dozer.properties
20 [main] WARN org.dozer.config.GlobalSettings - Dozer configuration file not found: dozer.properties.  Using defaults for all Dozer global properties.
20 [main] INFO org.dozer.DozerInitializer - Initializing Dozer. Version: 5.3.2, Thread Name: main
60 [main] INFO org.dozer.jmx.JMXPlatformImpl - Dozer JMX MBean [org.dozer.jmx:type=DozerStatisticsController] auto registered with the Platform MBean Server
70 [main] INFO org.dozer.jmx.JMXPlatformImpl - Dozer JMX MBean [org.dozer.jmx:type=DozerAdminController] auto registered with the Platform MBean Server
70 [main] INFO org.dozer.DozerBeanMapper - Initializing a new instance of dozer bean mapper.
Sachin


Jar Files Needed


commons-beanutils-1.7.jar
commons-logging-1.1.1-sources.jar
commons-logging-api-1.1.1.jar
dozer-5.3.2.jar
joda-time-2.2.jar
log4j-1.2.16.jar
log4j-over-slf4j-1.6.1.jar
org.apache.commons.lang_2.4.jar
slf4j.api-1.6.1.jar
slf4j-jdk14-1.6.1.jar
slf4j-simple-1.6.1.jar


Issues faced while developing the application

Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
java.lang.NoSuchMethodError: ch.qos.logback.classic.util.ContextInitializer.<init>(Lch/qos/logback/classic/LoggerContext;)V
at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:56)
at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:189)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:112)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:105)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:235)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:208)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:221)
at org.dozer.DozerBeanMapper.<clinit>(DozerBeanMapper.java:67)
at com.belazy.dozer.MainClass.main(MainClass.java:15)
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.dozer.stats.StatisticsManagerImpl.<init>(StatisticsManagerImpl.java:39)
at org.dozer.stats.GlobalStatistics.<init>(GlobalStatistics.java:29)
at org.dozer.stats.GlobalStatistics.<clinit>(GlobalStatistics.java:24)
at org.dozer.DozerBeanMapper.<clinit>(DozerBeanMapper.java:68)
at com.belazy.dozer.MainClass.main(MainClass.java:15)
Caused by: java.lang.SecurityException: class "org.slf4j.impl.MessageFormatter"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(Unknown Source)
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:114)
at ch.qos.logback.classic.Logger.filterAndLog(Logger.java:421)
at ch.qos.logback.classic.Logger.filterAndLog(Logger.java:430)
at ch.qos.logback.classic.Logger.info(Logger.java:494)
at org.dozer.config.GlobalSettings.loadGlobalSettings(GlobalSettings.java:113)
at org.dozer.config.GlobalSettings.<init>(GlobalSettings.java:67)
at org.dozer.config.GlobalSettings.<clinit>(GlobalSettings.java:46)
... 5 more



http://javabelazy.blogspot.in/

August 24, 2017

Copying one POJO object to another without clone

/**
 * MainClass
 */
package com.belazy.dozer;

import org.dozer.DozerBeanMapper;

/**
 * @author nijesh
 *
 */
public class MainClass {

public static void main(String[] args) {
DozerBeanMapper mapper = new DozerBeanMapper();
Employee employee = new Employee("Sachin", "tendulkar", 45);
EmployeeNew employeeNew = mapper.map(employee, EmployeeNew.class);
System.out.println(employeeNew.getEmployeeFirstName());
}

}

/**
 *
 */
package com.belazy.dozer;

/**
 * @author nijesh
 *
 */
public class Employee {

private String employeeFirstName;
private String employeeLastName;
private int employeeAge;

public Employee(String employeeFirstName,String employeeLastName, int employeeAge ) {
// TODO Auto-generated constructor stub
this.employeeAge = employeeAge;
this.employeeLastName = employeeLastName;
this.employeeFirstName = employeeFirstName;
}
/**
* @return the employeeFirstName
*/
public String getEmployeeFirstName() {
return employeeFirstName;
}
/**
* @param employeeFirstName the employeeFirstName to set
*/
public void setEmployeeFirstName(String employeeFirstName) {
this.employeeFirstName = employeeFirstName;
}
/**
* @return the employeeLastName
*/
public String getEmployeeLastName() {
return employeeLastName;
}
/**
* @param employeeLastName the employeeLastName to set
*/
public void setEmployeeLastName(String employeeLastName) {
this.employeeLastName = employeeLastName;
}
/**
* @return the employeeAge
*/
public int getEmployeeAge() {
return employeeAge;
}
/**
* @param employeeAge the employeeAge to set
*/
public void setEmployeeAge(int employeeAge) {
this.employeeAge = employeeAge;
}



}




/**
 *
 */
package com.belazy.dozer;

/**
 * @author nijesh
 *
 */
public class EmployeeNew {
private String employeeFirstName;
private String employeeLastName;
private int employeeAge;

public EmployeeNew(String employeeFirstName,String employeeLastName, int employeeAge ) {
// TODO Auto-generated constructor stub
this.employeeAge = employeeAge;
this.employeeLastName = employeeLastName;
this.employeeFirstName = employeeFirstName;
}
/**
* @return the employeeFirstName
*/
public String getEmployeeFirstName() {
return employeeFirstName;
}
/**
* @param employeeFirstName the employeeFirstName to set
*/
public void setEmployeeFirstName(String employeeFirstName) {
this.employeeFirstName = employeeFirstName;
}
/**
* @return the employeeLastName
*/
public String getEmployeeLastName() {
return employeeLastName;
}
/**
* @param employeeLastName the employeeLastName to set
*/
public void setEmployeeLastName(String employeeLastName) {
this.employeeLastName = employeeLastName;
}
/**
* @return the employeeAge
*/
public int getEmployeeAge() {
return employeeAge;
}
/**
* @param employeeAge the employeeAge to set
*/
public void setEmployeeAge(int employeeAge) {
this.employeeAge = employeeAge;
}





}

Jar files needed

dozer-5.4.0.jar
logback-classic-0.9.jar
logback-core-0.9.6.jar
slf4j.jar
slf4j-log4j13-1.0.1.jar


errors
Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
java.lang.NoSuchMethodError: ch.qos.logback.classic.util.ContextInitializer.<init>(Lch/qos/logback/classic/LoggerContext;)V
at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:56)
at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:189)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:112)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:105)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:235)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:208)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:221)
at org.dozer.DozerBeanMapper.<clinit>(DozerBeanMapper.java:67)
at com.belazy.dozer.MainClass.main(MainClass.java:15)
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.dozer.stats.StatisticsManagerImpl.<init>(StatisticsManagerImpl.java:39)
at org.dozer.stats.GlobalStatistics.<init>(GlobalStatistics.java:29)
at org.dozer.stats.GlobalStatistics.<clinit>(GlobalStatistics.java:24)
at org.dozer.DozerBeanMapper.<clinit>(DozerBeanMapper.java:68)
at com.belazy.dozer.MainClass.main(MainClass.java:15)
Caused by: java.lang.SecurityException: class "org.slf4j.impl.MessageFormatter"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(Unknown Source)
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:114)
at ch.qos.logback.classic.Logger.filterAndLog(Logger.java:421)
at ch.qos.logback.classic.Logger.filterAndLog(Logger.java:430)
at ch.qos.logback.classic.Logger.info(Logger.java:494)
at org.dozer.config.GlobalSettings.loadGlobalSettings(GlobalSettings.java:113)
at org.dozer.config.GlobalSettings.<init>(GlobalSettings.java:67)
at org.dozer.config.GlobalSettings.<clinit>(GlobalSettings.java:46)
... 5 more


http://javabelazy.blogspot.in/

Facebook comments