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

Bug: projects in eclipse are not visible

Sometimes after checking out projects from the repository, they are not visible in the package explorer.

Solution:

Do this in either the Project Explorer, Java, Java EE or the Enterprise Explorer view. In the view, click the little down-arrow to open a menu, and choose Deselect Working Set. This worked for me, and my project that was already there that wasn’t showing up now shows up

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.

Interface vs Abstract class

Interfaces

An interface is a contract: the guy writing the interface says, “hey, I accept things looking that way“, and the guy using the interface says “Ok, the class I write looks that way“.

An interface is an empty shell, there are only the signatures of the methods, which implies that the methods do not have a body. The interface can’t do anything. It’s just a pattern.
Implementing an interface consumes very little CPU, because it’s not a class, just a bunch of names, and therefore there is no expensive look-up to do. It’s great when it matters such as in embedded devices.

Abstract classes

Abstract classes, unlike interfaces, are classes. They are more expensive to use because there is a look-up to do when you inherit from them.

Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It’s more about a guy saying, “these classes should look like that, and they have that in common, so fill in the blanks!”.

e.g:

 

Eclipse optimization

It’s better to use latest version of Eclipse, which is Mars 2, as of now. Also it’s recommended to use Java 8 for faster loads. You can add this configuration to the eclipse.ini file, make sure that you put the correct path to javaw.exe:

-startup
plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar
–launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.300.v20150602-1417
-product
org.eclipse.epp.package.jee.product
–launcher.defaultAction
openFile
–launcher.XXMaxPermSize
512M
-showsplash
org.eclipse.platform
–launcher.XXMaxPermSize
512m
–launcher.defaultAction
-vm
C:/Program Files/Java/jdk1.8.0_74/bin/javaw.exe
openFile
–launcher.appendVmargs
-vmargs
-Dosgi.requiredJavaVersion=1.8
-Xms1536m
-Xmx2048m
-XX:+UseParallelGC
-Xverify:none

Conflicts with repository

It may happen that your code no longer is latest version. Therefore you need to get latest code from repository, but if you do so, the changes you made will be lost.
Therefore this is the steps needs to be done to remove the conflicts.

1. Try to commit your code
2. Eclipse will tell that your code is not latest from repository
3. Click the Synchronize SVN button
4. The files you wanted to commit become red
5. Open one of the files, look at the right corner of the compare window
6. If the little square there is blue(or looks like black) then you can proceed with automatic merger. it’s called “Copy All Non-Conflicting Changes from right to left”
7. Then right click on the file and “Mark as Merged”
8. The file is ready to be committed.

Note, if the square on the right top corner is red, you should do manual merge.

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.

 

@Override annotation

@Override annotation is placed above the method. This tells the compiler to use this methods instead the one in the super class. That means if you don’t have the method with same name in super class you don’t need to add @Override at all.

Beside that if you @override the method, compiler will warn you through the log, and this is useful to trace which method to use.