Saturday 27 August 2016

Java 8 JDBC

Java 8 : The JDBC-ODBC Bridge has been removed.

There is plenty of option to try. refer the Stackoverflow for futher references. 


Java 1.8 Concurrency feature "ConcurrentHashMap as a Set"

Java 1.8 Concurrency feature "ConcurrentHashMap as a Set"

The feature will return set of key in the map as Set.

public class VoiceConcurrentHashMap {

public static void main(String[] args) {

ConcurrentHashMap<String,String> map = new ConcurrentHashMap<String, String>();
System.out.println(map);
map.put("Lnkedin", "Networking");
map.put("Microsoft", "Windows10");
map.put("facebook", "Social");
map.put("Google", "SearchEngine");

KeySetView<String,String> set = map.keySet();
System.out.println(set);
}

}

Result : [Google, facebook, Microsoft, Lnkedin] .

Image :


ref :
Java API 


Behaviour of ConcurrentHashMap when it has Duplicate key.

Behaviour of ConcurrentHashMap when it has Duplicate key.

ConcurrentHashMap will overwrite previous value for a given key in Map. The object whos reference is lost are eligible for garage collection.

public class VoiceConcurrentHashMap {

public static void main(String[] args) {

ConcurrentHashMap<String,String> map = new ConcurrentHashMap<String, String>();
System.out.println(map);
map.put("Lnkedin", "profile");
map.put("Lnkedin", "profile");
map.put("Lnkedin", "jagdeesh");

}
}

ref :
1. Java API
2. Stackoverflow


Wednesday 24 August 2016

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

When supplying json object from postman. if Class contain a list object. Provide it as a array [].
ex :
{
    "name" : "raman",
    "employeeId" : 12345,
"orderList" : {"orderId" : 1,"orderName" : "rest json book"}
}
solution :
{
"name" : "raman",
"employeeId" : 12345,
"orderList" : [{"orderId" : 1,"orderName" : "rest json book"}]
}

@JsonIgnore

@JsonIgnore

If we want to serialize object to json, during this process some of the property can be null/empty can be ignored.
this can be achived using @jsonIgnore.

ex :
1. @JsonIgnore(Include.NON_NULL)
2. @JsonIgnore(Include.NON_EMPTY)
3. @JsonIgnore(Include.NON_DEFAULT)

Ref : wilddiary

use of @JsonSerialize

use of @JsonSerialize

A pojo class which is participating is serialisationtion should be annoted with @JsonSerialize

com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException


com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: org.apache.catalina.connector.CoyoteInputStream@76ab2e75; line: 3, column: 25] (through reference chain: com.group.dto.EmployeeDto["orderDto"])

Solution :
1. Json mapping should be done, when list object is added as property.

ex : Customer {
private int id;
private int name;
// missing json mapping annotation
private List<OrderDto> orderDto;
}
OrderDto {
pivate int orderId;
private String orderName;

}
2. Changes add @JsonProperty("orderList")

Customer {
private int id;
private int name;
// missing json mapping annotation
private List<OrderDto> orderDto;
}

Tuesday 23 August 2016

Please initialize the log4j system properly.

Eroor :

log4j:WARN No appenders could be found for logger (com.group.api.EmployeeService).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Solution :
1. In maven based. create log4j.properties file in folder /src/main/resources.
2. add following lines.

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n



Monday 22 August 2016

Failed to load class "org.slf4j.impl.StaticLoggerBinder".

Logging.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Solution :

1. Inlcude the following maven dependency.
       
       <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>1.7.21</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.7.21</version>
</dependency>




Sunday 21 August 2016

The ResourceConfig instance does not contain any root resource classes

When created a  maven web project.
1. Make sure to delete all the jsp file. 
2. Refresh and build

Method 2 :
1. In eclipse right click go to properties
2. Go ro java build path
3. Order and export
4. Add "JRE and system library","Maven library". and click ok
5. Right click on project
5.1 Go to run.
5.2 maven install.