Showing posts with label json. Show all posts
Showing posts with label json. 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



November 20, 2017

Rest call to Java Json sample program

Getting JSON Response from Rest Call in java

Description

This is a rest service call in java. The rest service which returns josn as response is saved to Java Object through Object Mapper API. The Object Mapper is very simple to use


/**
 *  Json to Java using Object Mapper
 */
package com.belazy.rest;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author itsection kozhikode
 *
 */
public class JsonMapper {

/**
* @param args
* @throws IOException
* @throws JsonMappingException
* @throws JsonGenerationException
*/
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
// TODO Auto-generated method stub

ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("bmw","blue");
File resultFile = new File("d://belazy.json");
objectMapper.writeValue(resultFile, car);

ComputerDetails comp = objectMapper.readValue(new URL("http://ip.jsontest.com"), ComputerDetails.class);
System.out.println(comp.getIp());

JsonKey jsonkey = objectMapper.readValue(new URL("http://echo.jsontest.com/key/value/one/two"), JsonKey.class);
System.out.println("jsonkey.getOne() : "+jsonkey.getOne());

}





}


computerdetails.class




/**
 *
 */
package com.belazy.rest;

/**
 * @author belazy
 *
 */
public class ComputerDetails {

String ip;

/**
* @return the ip
*/
public String getIp() {
return ip;
}

/**
* @param ip the ip to set
*/
public void setIp(String ip) {
this.ip = ip;
}


}

jsonkey.class


package com.belazy.rest;
public class JsonKey
{
    private String one;

    private String key;

    public String getOne ()
    {
        return one;
    }

    public void setOne (String one)
    {
        this.one = one;
    }

    public String getKey ()
    {
        return key;
    }

    public void setKey (String key)
    {
        this.key = key;
    }

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

java api needed



jackson annotation jar
jackson core
jackson databind



Output



86.99.219.5
jsonkey.getOne()two

November 19, 2017

How to deal with SSL Handshake exception

/**
 * Consuming a Rest application
 */
package com.belazy.rest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author belazy
 *
 * This program I dedicate to my Tech Arc
 * she wish be a data scientist
 * Hope she will achieve it in the nearest days.
 *
 */
public class RestConsumer {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

String theUrl = "https://jsonplaceholder.typicode.com/posts";

try {
URL url = new URL(theUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

System.out.println(conn.getResponseCode());

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


/**
 * 
 */
package com.belazy.rest;

/**
 * @author belazy
 *
 */
public class Post {
private String userId;
private String id;
private String title;
private String body;

/**
* @return the userId
*/
public String getUserId() {
return userId;
}

/**
* @param userId the userId to set
*/
public void setUserId(String userId) {
this.userId = userId;
}

/**
* @return the id
*/
public String getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}

/**
* @return the title
*/
public String getTitle() {
return title;
}

/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}

/**
* @return the body
*/
public String getBody() {
return body;
}

/**
* @param body the body to set
*/
public void setBody(String body) {
this.body = body;
}

}





Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at com.belazy.rest.RestConsumer.main(RestConsumer.java:38)

Facebook comments