Thursday, January 30, 2014

How To Convert JSON Object To / From Java


How To Convert JSON Object To / From Java


ADF does not support REST WebService with  JSON Object as response, it supports only XML as Response.

Jackson is a High-performance JSON processor Java library. In this tutorial, we show you how to use Jackson’s data binding to convert Java object to / from JSON.
For object/json conversion, you need to know following two methods :
//1. Convert Java object to JSON format
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File("c:\\user.json"), user);
//2. Convert JSON to Java object
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(new File("c:\\user.json"), User.class);
Note
Both writeValue() and readValue() has many overloaded methods to support different type of inputs and outputs. Make sure check it out.

1. Jackson Dependency

Jackson contains 6 separate jars for different purpose, check here. In this case, you only need “jackson-mapper-asl” to handle the conversion, just declares following dependency in your pom.xml
  
 
  codehaus
http://repository.codehaus.org/org/codehaus   org.codehaus.jackson jackson-mapper-asl 1.8.5
For non-maven user, just get the Jackson library here.

2. POJO

An user object, initialized with some values. Later use Jackson to convert this object to / from JSON.
package com.mkyong.core;
 
import java.util.ArrayList;
import java.util.List;
 
public class User {
 
 private int age = 29;
 private String name = "mkyong";
 private List<String> messages = new ArrayList<String>() {
  {
   add("msg 1");
   add("msg 2");
   add("msg 3");
  }
 };
 
 //getter and setter methods
 
 @Override
 public String toString() {
  return "User [age=" + age + ", name=" + name + ", " +
    "messages=" + messages + "]";
 }
}

3. Java Object to JSON

Convert an “user” object into JSON formatted string, and save it into a file “user.json“.
package com.mkyong.core;
 
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
 
public class JacksonExample {
    public static void main(String[] args) {
 
 User user = new User();
 ObjectMapper mapper = new ObjectMapper();
 
 try {
 
  // convert user object to json string, and save to a file
  mapper.writeValue(new File("c:\\user.json"), user);
 
  // display to console
  System.out.println(mapper.writeValueAsString(user));
 
 } catch (JsonGenerationException e) {
 
  e.printStackTrace();
 
 } catch (JsonMappingException e) {
 
  e.printStackTrace();
 
 } catch (IOException e) {
 
  e.printStackTrace();
 
 }
 
  }
 
}
Output
{"age":29,"messages":["msg 1","msg 2","msg 3"],"name":"mkyong"}
Note
Above JSON output is hard to read. You can enhance it by enable the pretty print feature.

4. JSON to Java Object

Read JSON string from file “user.json“, and convert it back to Java object.
package com.mkyong.core;
 
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
 
public class JacksonExample {
    public static void main(String[] args) {
 
 ObjectMapper mapper = new ObjectMapper();
 
 try {
 
  // read from file, convert it to user class
  User user = mapper.readValue(new File("c:\\user.json"), User.class);
 
  // display to console
  System.out.println(user);
 
 } catch (JsonGenerationException e) {
 
  e.printStackTrace();
 
 } catch (JsonMappingException e) {
 
  e.printStackTrace();
 
 } catch (IOException e) {
 
  e.printStackTrace();
 
 }
 
  }
 
}
User [age=29, name=mkyong, messages=[msg 1, msg 2, msg 3]]

References

  1. Download Jackson
  2. Jackson Official Site
  3. Gson example – convert Java object to / from JSON

Tuesday, January 28, 2014

ADF: HOW TO CREATE A CUSTOM ADF LOGIN FORM ON WEBLOGIC

ADF: HOW TO CREATE A CUSTOM ADF LOGIN FORM ON WEBLOGIC


This blog demonstrates how to create an ADF login page using a managed bean to capture the user's credentials. It then calls the adfAuthentication servlet to perform the ADF authentication.

Follow the Steps mentioned in the video ADF LOGIN VIDEO  OR

DOWNLOAD THE PROJECT FROM THIS LINK CUSTOMIZED ADF LOGIN PROJECT

ADF : How to Display ADF View Response in Tree Form

ADF : How to Display ADF View Response in  Tree Form


Follow the Steps as mentioned in the Video Youtube Video  OR

Download the Completed Application From Here DOWNLOAD ADF PROJECT





ADF Tips : How to Modify Default Context Root in ADF Application

ADF:  How to Modify Default Context Root in ADF Application

When you start running ADF applications under JDev 11g you'll become familiar with seeing URLs like the following in your browser:

http://127.0.0.1:7101/Sage-ViewController-context-root/faces/Welcome.jspx

Each part of the URL is significant, though the one that stands out as being odd is the rather cumbersome "Sage-ViewController-context-root". This part of the URL in Java EE parlance is called the context-root, and not just because our example literally has context-root written into it.

The context-root at its most basic form is the unique part of the URL on the application server to clearly distinguish your application from another on the server. However as you can see in this example the "Sage-ViewController-context-root" is hardly something you'd want to present to users. Something more palatable like "Sage" or "HR" would be useful, delivering an end URL like:

http://127.0.0.1:7101/Sage/faces/Welcome.jspx

So how do we go about making a friendly context-root for our applications? Essentially there's 2 locations.

Design time

If you're running your application from JDeveloper by opening & running a jspx page via the right mouse menu or similar, when the application is deployed to the integrated WLS server, it will use Java EE Application options under the ViewController's Project Properties, accessible by right clicking the same name option of the ViewController project:

As you can see in the above picture, the Java EE Web Context Root is set to "Sage-ViewController-context-root". Simply change this to something desired.

Deploy time

Once you've finished testing and running your application in your local integrated WLS instance, you're likely to generate an EAR or WAR file to deploy to an external WLS server. In this case the context-root is influenced by settings in the WAR file for the ViewController project. Simply right click the ViewController project -> select Project Properties -> Deployment and locate the associated WAR file deployment profile. In the following picture you can see it's called "Sage_ViewController_webapp1":

If you then select the edit button for the WAR deployment profile, the first node General includes 2 options, namely Use Project's Java EE Web Context Root and Specify Java EE Web Context Root. If you select the 2nd option and enter your own option, this will override the context-root once the application is deployed to an external WLS server:

Friday, January 24, 2014

RESTFul WebService Development and Consumption With Oracle ADF

Popular Posts