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:

 

[code language=”java”]
@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;
}
[/code]

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

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.

 

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

[code language=”java”]
// 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");
[/code]

@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:

[code language=”java”]
String jndiPath = com.development.tutorials.ejb3.RemoteCalculator;

try{

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

}
[/code]

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

Accessing Session Beans

After we implemented the session beans, we should be able to access it.

EJB interactions:

A client for and EJB can be:

  1. Java EE Application Client
  2. Java EE component (JSP,Servlet,JSF,EJB)
  3. Any java object
  4. Java application

Client finds a proxy to interact with an EJB using:

  1. Java naming and directory interfaces(JDNI)
  2. Dependency Injection(used most of the time)

Steps for interacting with with and EJB(3-step plan)

  1. Declare the reference for the EJB interface
  2. Get the EJB “instance”
    1. using JNDI for remote clients or legacy EJBs
    2. using DI for local clients or application clients
  3. Invoke methods on the interface

 

Session Bean Implementations

To implement the local stateless bean

  1. import javax.ejb.Stateless;
  2. add the annotation above the class: @Stateless  to see what is the difference between beans check my Choosing and developing a Session Bean

To implement the remote statelful bean

  1. import javax.ejb.Statelful;
  2. add the annotation above the class: @Stateful

To implement the local singleton bean

  1. import javax.ejb.LocalBean;
  2. import javax.ejb.Singleton;
  3. add the annotation above the class:
    1. @LocalBean
    2. @Singleton

Every session bean has:

  1. Business interface
  2. Implementation class
  3. Deployment descriptor information