Json to Java object conversion

Let’s say you have rest service that posts some data to your server. The rest services uses JSON format to send data, but your server might not understand the JSON, therefore you will need to convert the JSON to Java object. Below is the sample of such service:

 

@POST
	@Path("/statistics")
	@Consumes(APPLICATION_JSON_UTF8)
	public Response getStatistics(String json)
												throws JsonGenerationException,
												JsonMappingException,

												IOException {

		ObjectMapper mapper = new ObjectMapper();
		
		SomeCustomerTO someUser = mapper.readValue(json, SomeCustomerTO .class);
		return null;
	}

Always good idea to include some logging to see if you receiving the correct json

Advertisements

Adding the Oracle connectors to IntelliJ IDEA

Libraries cannot be directly used in any program if not properly added to the project gradle files.

This can easily be done in smart IDEs like inteli J.

1) First as a convention add a folder names ‘libs’ under your project src file. (this can easily be done using the IDE itself)

2) then copy or add your library file (eg: .jar file) to the folder named ‘libs’

3) now you can see the library file inside the libs folder. Now right click on the file and select ‘add as library’. And this will fix all the relevant files in your program and library will be directly available for your use.

Static block

Static blocks are nothing but a normal block of code, enclosed in braces { }, preceded with static keyword. These static blocks will be called when JVM loads the class into memory. Incase a class has multiple static blocks across the class, then JVM combines all these blocks as a single block of code and executes it. Static blocks will be called only once, when it is loaded into memory. These are also called initialization blocks.

package com.java2novice.staticexmp;
 
import java.util.ArrayList;
import java.util.List;
 
public class MyStaticBlock {
 
    private static List<String> list;
     
    static{
        //created required instances
        //create ur in-memory objects here
        list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
    }
     
    public void testList(){
        System.out.println(list);
    }
     
    public static void main(String a[]){
        MyStaticBlock msb = new MyStaticBlock();
        msb.testList();
    }
}

 

Output:
[one, two, three, four]

Java annotations

Java annotations are one of the main ease-of-development features introduced in JDK 5. Annotations are like meta-tags that you can add to your code and apply to package declarations, type declarations, constructors, methods, fields, parameters and variables. They provide a helpful way to indicate whether your methods are dependent on other methods, whether they are incomplete, whether your classes have references to other classes, and so on.

Dependecny Injection(DI) to Access EJBs

There are two ways to access the EJBs: 1. JNDI 2. DI

Advantages:

  1. Removes JNDI lookups
  2. DI can inject this:
    1. @EJB
    2. @Resource, @PersistenceContext, @EntityManager

DI is supported within:

  1. Enterprise Java Beans
  2. Servlets/JSPs/Filters/Managed Beans/
  3. Applicaiton Client Container clients

Annotations:

@EJB

Example of dependency injection EJB -> Servlet

// this is servlet
@WebServlet(name="DICalculator", urlPatterns = {"/DICalculator"})
public class DICalculatorServerlet extends HttpServlet {

// Line below injects RemoteCalculator bean into this servlet
@EJB
private RemoteCalculator calculator;
// now we can use the injected object
int sum = calculator.multiply(5,5);
RequestDispatcher dispatcher = request.getRequestDispatcher("calc-result.jsp");

@Resource

JNDI

Java Naming and Naming Interface if part of Java SE:

  1. Standard API to interact with naming and directory services
  2. Provide the ability to look up things in a “registry”
  3. Used in enterprise solutions to locate resources such as EJBs, JMS queues or Web Services

JNDI resources are organized in tree structure:

  1. Analogues to the folder structure of a computer’s file system
  2. Supports events, lookups, and complex searching against structure

App->JNDI->JNDI IMPL->DS

Finding EJB with JNDI uses four key concepts:

  1. Context
  2. Initial context
  3. Path & Name
  4. Search

Simple JNDI lookup:

String jndiPath = com.development.tutorials.ejb3.RemoteCalculator;

try{

   calculator = (RemoteCalculator) c.lookup(jndiPath);

}

Problems with JNDI
1. Keep track of JNDI path
2. Bean name